Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void stripSpaces ( char * str )
  6. {
  7. char * dst, * str_src;
  8. int inSpaces = 1;
  9. dst = str, str_src = str;
  10.  
  11. while ( * str_src )
  12. {
  13. if ( inSpaces )
  14. {
  15. // Copies everything expcept for spaces
  16. if ( * str_src != ' ' && * str_src != '\t' )
  17. {
  18. * dst ++ = * str_src;
  19. inSpaces = 0;
  20. }
  21. }
  22. else
  23. {
  24. /**
  25. * If not in spaces, this copies everything, including one space.
  26. * If theres more than one space, this switches to "inSpaces" mode.
  27. **/
  28. * dst ++ = * str_src;
  29. if ( * str_src == ' ' || * str_src == '\t' ) inSpaces = 1;
  30. }
  31. str_src ++;
  32. }
  33.  
  34. // If str was an empty string, this prevents it from moving one character back.
  35. if ( inSpaces && dst > str ) dst --;
  36.  
  37. * dst = 0;
  38. }
  39.  
  40. void removeLF ( char * str )
  41. {
  42. int len = strlen ( str ), i = 0, start = 0, end = 0;
  43.  
  44. // Sets start to the first letter of string
  45. while ( len > start && ( str[start] == '\n' || str[start] == ' ' ) ) start ++;
  46.  
  47. // Sets end to the last letter of string
  48. while ( len > end && str[len-end-1] == '\n' ) str[len-end++-1] = 0;
  49. end = len-end-1;
  50.  
  51. // Aligns string to the left if not already aligned and trims newlines
  52. while ( start <= len )
  53. {
  54. if ( start <= end )
  55. {
  56. if ( str[start] != '\n' ) str[i] = str[start];
  57. else
  58. {
  59. while ( str[start+1] == '\n' )
  60. if ( str[start+2] == '\n' ) start ++;
  61. else
  62. {
  63. str[i] = '\n';
  64. i ++;
  65. start ++;
  66. break;
  67. }
  68. str[i] = ' ';
  69. }
  70. }
  71. else str[i] = 0;
  72. i ++, start ++;
  73. }
  74.  
  75. }
  76.  
  77. int isDel ( char c )
  78. {
  79. // Detects if char is a delimiter (1) or not (0)
  80. switch ( c )
  81. {
  82. case '\0':
  83. case '\t':
  84. case ' ':
  85. return 1;
  86. break;
  87. default:
  88. return 0;
  89. }
  90. }
  91.  
  92. void printLine ( const char * start, const char * end )
  93. {
  94. const char * p = start;
  95. while ( p <= end ) putchar ( * p ++ );
  96. putchar ( '\n' );
  97. }
  98.  
  99. int setLength ( char * str, int width )
  100. {
  101. width ++;
  102. int chars = 1;
  103.  
  104. char * start = & str[0],
  105. * end = & str[0],
  106. * current = & str[0];
  107.  
  108. while ( * current != '\0' )
  109. {
  110. while ( chars <= width )
  111. {
  112. while ( ! isDel ( * current ) )
  113. ++ current, ++ chars;
  114.  
  115. if ( chars <= width )
  116. {
  117. if ( * current == '\0' )
  118. {
  119. puts ( start );
  120. return 0;
  121. }
  122. end = current - 1;
  123. current ++, chars ++;
  124. }
  125. }
  126.  
  127. if ( end == start )
  128. end = current - 1;
  129.  
  130. printLine ( start, end );
  131. current = end + 1;
  132.  
  133. while ( isDel ( * current ) )
  134. {
  135. if ( * current == '\0' ) return 0;
  136. else ++ current;
  137. }
  138. start = current;
  139. end = current;
  140. chars = 1;
  141. }
  142.  
  143. return 0;
  144. }
  145.  
  146. char * wordWrap ( int width, const char * src )
  147. {
  148. if ( width < 0 ) return NULL;
  149.  
  150. // Allocates mem for the copy of src string and copies it to src_tp
  151. char * src_tp;
  152. src_tp = ( char * ) calloc ( strlen ( src ) + 1, sizeof ( char ) );
  153. strcpy ( src_tp, src );
  154.  
  155. // Strips spaces and directly modifies the src_tp string
  156. stripSpaces ( src_tp );
  157.  
  158. // Trims newlines and directly modifies the src_tp string
  159. removeLF ( src_tp );
  160.  
  161. // Sets line length to specified width, NULL on error
  162. setLength ( src_tp, width );
  163.  
  164. return src_tp;
  165. }
  166.  
  167. int main ( void )
  168. {
  169. char * res;
  170.  
  171. const char * s0 =
  172. "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer metus\n"
  173. "pede, pretium vitae, rhoncus et, auctor sit amet, ligula. Integer volutpat\n"
  174. "orci et elit. Nunc tempus, urna at sollicitudin rutrum, arcu libero rhoncus\n"
  175. "lectus, vitae feugiat purus orci ultricies turpis. Pellentesque habitant\n"
  176. "morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nam\n"
  177. "in pede. Etiam eu sem id urna ultricies congue. Vestibulum porttitor\n"
  178. "ultrices neque. Mauris semper, mauris ut feugiat ultricies, augue purus\n"
  179. "tincidunt elit, eu interdum ante nisl ac ante. Pellentesque dui. Vestibulum\n"
  180. "pretium, augue non cursus pretium, nibh dolor laoreet leo, sed pharetra pede\n"
  181. "libero non diam.";
  182.  
  183. res = wordWrap ( 40, s0 );
  184.  
  185. //puts ( res );
  186.  
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement