Write a program that replaces two or more consecutive blanks in a string by a single blank

C program to remove spaces or excess blanks from a string, For example, consider the string

"C programming"

There are two spaces in this string, so our program will print the string
"C programming." It will remove spaces when they occur more than one time consecutively in string anywhere.

C program

#include <stdio.h> 

int main()


{
   char text[1000], blank[1000];
   int c = 0, d = 0; 

   printf("Enter some text\n");


   gets(text); 

   while (text[c] != '\0') {


      if (text[c] == ' ') {
         int temp = c + 1;
         if (text[temp] != '\0') {
            while (text[temp] == ' ' && text[temp] != '\0') {
               if (text[temp] == ' ') {
                  c++;
               }  
               temp++;
            }
         }
      }
      blank[d] = text[c];
      c++;
      d++;
   } 

   blank[d] = '\0';

 

   printf("Text after removing blanks\n%s\n", blank);

 

   return 0;


}

If you want you can copy blank into text string so that original string is modified.

Download Remove spaces program.

Output of program:

Write a program that replaces two or more consecutive blanks in a string by a single blank

C programming code using pointers

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SPACE ' ' 

char *process(char*);

 

int main()


{
   char text[1000], *r; 

   printf("Enter a string\n");


   gets(text); 

   r = process(text);

   printf("\"%s\"\n", r);   

   free(r);    

 

   return 0;


}

char *process(char *text) {
   int length, c, d;
   char *start;   

   c = d = 0;

   

   length = strlen(text);

 

   start = (char*)malloc(length+1);

   

   if (start == NULL)


      exit(EXIT_FAILURE);   

   while (*(text+c) != '\0') {


      if (*(text+c) == ' ') {
         int temp = c + 1;
         if (*(text+temp) != '\0') {
            while (*(text+temp) == ' ' && *(text+temp) != '\0') {
               if (*(text+temp) == ' ') {
                  c++;
               }  
               temp++;
            }
         }
      }
      *(start+d) = *(text+c);
      c++;
      d++;
   }
   *(start+d)= '\0';   

   return start;


}

Write a C program to remove extra spaces, blanks from a string. How to remove extra blank spaces, blanks from a given string using functions in C programming. Logic to remove extra white space characters from a string in C.

Example

Input

Input string: Learn C programming at Codeforwin.

Output

String after removing extra blanks: "Learn C programming at Codeforwin"

Required knowledge

Basic C programming, Loop, String, Function

Must know - Program to copy one string to another

Logic to remove extra spaces, blanks

  1. Input string from user, store it in some variable say str.
  2. Declare another string newString with same size as of str, to store string without blanks.
  3. For each character in str copy to newString. If current character in str is white space. Then copy single white space to newString and rest white spaces in str.

Program to remove extra spaces from string

/** * C program to remove extra blank spaces from a given string */ #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // Maximum string size /* Function declaration */ char * removeBlanks(const char * str); int main() { char str[MAX_SIZE]; char * newString; printf("Enter any string: "); gets(str); printf("\nString before removing blanks: \n'%s'", str); newString = removeBlanks(str); printf("\n\nString after removing blanks: \n'%s'", newString); return 0; } /** * Removes extra blank spaces from the given string * and returns a new string with single blank spaces */ char * removeBlanks(const char * str) { int i, j; char * newString; newString = (char *)malloc(MAX_SIZE); i = 0; j = 0; while(str[i] != '\0') { /* If blank space is found */ if(str[i] == ' ') { newString[j] = ' '; j++; /* Skip all consecutive spaces */ while(str[i] == ' ') i++; } newString[j] = str[i]; i++; j++; } // NULL terminate the new string newString[j] = '\0'; return newString; }

Enter any string: Learn C programming at Codeforwin. String before removing blanks: 'Learn C programming at Codeforwin.' String after removing blanks: 'Learn C programming at Codeforwin.'

Happy coding 😉