Remove all extra blank spaces from given string
C program to remove all extra blank spaces from given string.
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
void remove_extra_spaces(char str[MAX], char words[MAX][MAX])
{
int i, j, len;
char prev_char;
prev_char = '\0';
len = strlen(str);
for(i=0;i<len;i++)
{
if(str[i] == '\t')
{
str[i] = ' ';
}
}
i = 0;
while(1)
{
if(str[i] == ' ')
{
if(prev_char==' ')
{
for(j=i;j<len;j++)
{
str[j] = str[j+1];
}
len--;
str[len] = '\0';
i--;
}
else
{
prev_char = str[i];
}
}
else
{
prev_char = str[i];
}
if(str[i] == '\0')
break;
else
i++;
}
printf("\nModified string\t:%s",str);
}
void main()
{
char str[MAX];
char words[MAX][MAX];
printf("Enter the string\t:");
gets(str);
remove_extra_spaces(str, words);
getch();
}
Output
Enter the string :welcome to coursecrux.com, happy coding!!!
Modified string :welcome to coursecrux.com, happy coding!!!