Practice

Compress



Write a function with the header

 int compress(char str[ ])

that receives the address of a null-terminated string str[], removes any leading blanks in str[], any extra blanks between the words in str[] and any trailing blanks in str[] and returns the number of characters in the compressed string (excluding the null byte).  A word is any set of consecutive non-whitespace characters.  Any two consecutive words are separated from one another by whitespace characters. 

The statements

 int i;
 char str[ ] = "   This is    a     test.      ";

 i = compress(str);
 printf ("'%s' contains %d characters.\n", str, i);

displays

 'This is a test.' contains 15 characters.
  Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site