Write a C function remove_blanks()that replaces two or more consecutive blanks in the input string by a single blank. For example, if the input is:
“Grim return to the planet of apes!!”
Then output should be:
“Grim return to the planet of apes!!”
The function should return the number of blank spaces removed. Remember that string is a null-terminated character array! The function prototype is given below:
int remove_blanks(char input_string[],char output_string[]);
#include<stdio.h>
int remove_blanks(char input_string[]);
int main()
{
char c[100];
printf("Enter a string ");
fgets(c,100,stdin);
printf("Spaces removed = %d",remove_blanks(c));
return 0;
}
int remove_blanks(char input_string[])
{
int c = 0, d = 0,n = 0;
char blank[100];
while (input_string[c] != '\0')
{
if (input_string[c] == ' ')
{
int temp = c + 1;
if (input_string[temp] != '\0')
{
while (input_string[temp] == ' ' && input_string[temp] != '\0')
{
if (input_string[temp] == ' ')
{
c++;
n++;
}
temp++;
}
}
}
blank[d] = input_string[c];
c++;
d++;
}
printf("Output string : %s",blank);
return n;
}
Comments
Leave a comment