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[]);
int remove_blank(char input_string[], char output_string[])
{
char* a = input_string;
int removedBlanks = 0;
char* b = output_string;
bool isAlreadyBlank = 0;
while (*a != 0)
{
if (*a == ' ')
{
if (!isAlreadyBlank)
{
*b = *a;
b++;
isAlreadyBlank = 1;
}
else removedBlanks++;
}
else
{
*b = *a;
b++;
isAlreadyBlank = 0;
}
a++;
}
*b = 0;
return removedBlanks;
}
Comments
Leave a comment