Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void stripSpaces ( char * str )
- {
- char * dst, * str_src;
- int inSpaces = 1;
- dst = str, str_src = str;
- while ( * str_src )
- {
- if ( inSpaces )
- {
- // Copies everything expcept for spaces
- if ( * str_src != ' ' && * str_src != '\t' )
- {
- * dst ++ = * str_src;
- inSpaces = 0;
- }
- }
- else
- {
- /**
- * If not in spaces, this copies everything, including one space.
- * If theres more than one space, this switches to "inSpaces" mode.
- **/
- * dst ++ = * str_src;
- if ( * str_src == ' ' || * str_src == '\t' ) inSpaces = 1;
- }
- str_src ++;
- }
- // If str was an empty string, this prevents it from moving one character back.
- if ( inSpaces && dst > str ) dst --;
- * dst = 0;
- }
- void removeLF ( char * str )
- {
- int len = strlen ( str ), i = 0, start = 0, end = 0;
- // Sets start to the first letter of string
- while ( len > start && ( str[start] == '\n' || str[start] == ' ' ) ) start ++;
- // Sets end to the last letter of string
- while ( len > end && str[len-end-1] == '\n' ) str[len-end++-1] = 0;
- end = len-end-1;
- // Aligns string to the left if not already aligned and trims newlines
- while ( start <= len )
- {
- if ( start <= end )
- {
- if ( str[start] != '\n' ) str[i] = str[start];
- else
- {
- while ( str[start+1] == '\n' )
- if ( str[start+2] == '\n' ) start ++;
- else
- {
- str[i] = '\n';
- i ++;
- start ++;
- break;
- }
- str[i] = ' ';
- }
- }
- else str[i] = 0;
- i ++, start ++;
- }
- }
- int isDel ( char c )
- {
- // Detects if char is a delimiter (1) or not (0)
- switch ( c )
- {
- case '\0':
- case '\t':
- case ' ':
- return 1;
- break;
- default:
- return 0;
- }
- }
- void printLine ( const char * start, const char * end )
- {
- const char * p = start;
- while ( p <= end ) putchar ( * p ++ );
- putchar ( '\n' );
- }
- int setLength ( char * str, int width )
- {
- width ++;
- int chars = 1;
- char * start = & str[0],
- * end = & str[0],
- * current = & str[0];
- while ( * current != '\0' )
- {
- while ( chars <= width )
- {
- while ( ! isDel ( * current ) )
- ++ current, ++ chars;
- if ( chars <= width )
- {
- if ( * current == '\0' )
- {
- puts ( start );
- return 0;
- }
- end = current - 1;
- current ++, chars ++;
- }
- }
- if ( end == start )
- end = current - 1;
- printLine ( start, end );
- current = end + 1;
- while ( isDel ( * current ) )
- {
- if ( * current == '\0' ) return 0;
- else ++ current;
- }
- start = current;
- end = current;
- chars = 1;
- }
- return 0;
- }
- char * wordWrap ( int width, const char * src )
- {
- if ( width < 0 ) return NULL;
- // Allocates mem for the copy of src string and copies it to src_tp
- char * src_tp;
- src_tp = ( char * ) calloc ( strlen ( src ) + 1, sizeof ( char ) );
- strcpy ( src_tp, src );
- // Strips spaces and directly modifies the src_tp string
- stripSpaces ( src_tp );
- // Trims newlines and directly modifies the src_tp string
- removeLF ( src_tp );
- // Sets line length to specified width, NULL on error
- setLength ( src_tp, width );
- return src_tp;
- }
- int main ( void )
- {
- char * res;
- const char * s0 =
- "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer metus\n"
- "pede, pretium vitae, rhoncus et, auctor sit amet, ligula. Integer volutpat\n"
- "orci et elit. Nunc tempus, urna at sollicitudin rutrum, arcu libero rhoncus\n"
- "lectus, vitae feugiat purus orci ultricies turpis. Pellentesque habitant\n"
- "morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nam\n"
- "in pede. Etiam eu sem id urna ultricies congue. Vestibulum porttitor\n"
- "ultrices neque. Mauris semper, mauris ut feugiat ultricies, augue purus\n"
- "tincidunt elit, eu interdum ante nisl ac ante. Pellentesque dui. Vestibulum\n"
- "pretium, augue non cursus pretium, nibh dolor laoreet leo, sed pharetra pede\n"
- "libero non diam.";
- res = wordWrap ( 40, s0 );
- //puts ( res );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement