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 “conio.h”
#include “string.h”
void main()
{
char input[100],output[100],c;
int i=0,j=0;
clrscr();
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];
}
output[j]='';
printf("\noutput string is\n");
puts(output);
getch();
}

Output:

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

Leave a Reply