Answer to Question #195407 in C for Hassam

Question #195407

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[]);


1
Expert's answer
2021-05-20T20:25:55-0400
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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS