Word Wrap

Text editors wrap words to fit within a prescribed width, sometimes represented by a number of columns.  If a word at the end of a line does not fit within the prescribed number of columns, then that the text editor word starts the word on a newline.  A word is defined as a set non-whitespace characters.  A word is separated from any other word by whitespace. 

Write a function with the header

 int wordWrap(char s[ ], int width) 

that receives the address of a null-terminated string s[ ] and a number of columns width.  This string may contain whitespace characters adjacent to one another.  Your function

  • changes s[ ] to a wrapped version that fits within width
  • stores the wrapped version in s[ ]
  • returns the number of lines that the wrapped version occupies

If any word in the original string exceeds the prescribed width, wordWrap breaks that word into parts.

The statements

 char s[ ] = "My home is in Toronto    Ontario";
 int i;

 i =  wordWrap(s, 7);
 printf("%d\n%s\n", s);


 My home
 is in
  Top  Go Back to the Top of this Page

  Designed by Chris Szalwinski   Copying From This Site