Strings: Delete Trailing Blanks and Tabs

bumsfeld 0 Tallied Votes 1K Views Share

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.