Tuesday, October 16, 2012

C program to replace multiple spaces with a single space


Program to replace multiple spaces between two words in a string by a single space


#include<stdio.h>

#include<conio.h>

void main()

{
char s[50];
char *str=s;
int i,j;
clrscr();
printf("Enter the String:\n");
gets(s);
for(i=0;*(str+i)!='\0';i++)
   {
   if(*(str+i)==' '&*(str+i+1)==' ')
   {

   for(j=i;*(str+j)!='\0';j++)
     {
 *(str+j)=*(str+j+1);
 }
 i--;
   }

   }
     printf("\n%s",str);
   getch();
}

No comments: