by Sai gowtham

The C program to remove white spaces from a string

This example program shows you, how to remove multiple white spaces from a string with single white space in C language.

#include <stdio.h>

int main()
{
   char str[100], final[100];
   int c = 0, d = 0;

   printf("Enter some text\n");
   fgets(str, sizeof str, stdin);

   while (str[c] != '\0')
   {
      if (!(str[c] == ' ' && str[c+1] == ' ')) {
        final[d] = str[c];
        d++;
      }
      c++;
   }

   str[d] = '\0';

   printf("Text after removing the spaces\n%s\n", final);

   return 0;
}

Output:

Enter some text
Good    morning    Olive

Text after removing the spaces

Good morning Olive

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course