Answer to Question #195435 in C for ABDULQADIR BUTT

Question #195435

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-19T15:51:33-0400
#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;
}

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