The C program to remove white spaces from a string
This example program shows you, how to remove multiple white spaces from a string with single white space in C language.
#include <stdio.h>
int main()
{
char str[100], final[100];
int c = 0, d = 0;
printf("Enter some text\n");
fgets(str, sizeof str, stdin);
while (str[c] != '\0')
{
if (!(str[c] == ' ' && str[c+1] == ' ')) {
final[d] = str[c];
d++;
}
c++;
}
str[d] = '\0';
printf("Text after removing the spaces\n%s\n", final);
return 0;
}
Output:
Enter some text
Good morning Olive
Text after removing the spaces
Good morning Olive