Program to replace each string of one or more blanks by a single blank

Here is the program to replace each string of one or more blanks by a single blank.

#include "stdio.h"
#include "string.h"
int
main ()
{
  char input[100], output[100], c;
  int i = 0, j = 0;
  printf ("Enter the string\n");
  gets (input);
  for (i = 0; i < strlen (input); i++)
    {
      if (input[i] == ' ' && input[i + 1] != ' ')
          output[j++] = input[i];
      else if (input[i] != ' ')
          output[j++] = input[i];
    }

  printf ("\noutput string is\n");
  puts (output);
}

Output:

Enter the string
How          are                 you         ?
Output string is: How are you ? 

Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in