Start New Discussion within our Software Development Community

This program use a while loop to delete trailing spaces and tabs from a string. It is meant to be more of an example of the application of the while loop.

/* while loop to remove trailing blanks and tabs */

#include <stdio.h>
#include <string.h>

int main()
{
  int n;
  char line[55];
  
  strcat ( line, "the quick green frog ...           " );
  printf ( "%s%c\n",line,'!' );
  n = strlen ( line );
  /* go from the end */
  while ( --n >= 0 )
  {
    if (line[n] != ' ' && line[n] != '\t' && line[n] != '\n')
    {
    	break;
    }
  }
  line[n + 1] = '\0';  /* add end of string character */
  printf ( "%s%c\n", line, '!');

  getchar();  /* wait for key */
  return 0;
}
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.