Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C Program to Remove Extra Spaces from Given String

    C Programming Examples


    This C program removes extra spaces from a user-entered string. It checks for additional blank characters inside looping using a conditional statement, skips it if found, and prints the final output to the screen.

    Usually, extra spaces are not needed when taking input from users. So it is good to filter and remove unnecessary spaces before further processing.

    Example C Program:
    #include <stdio.h>
    
    void main() {
      /* Variable Declaration and Initialization. */
      char inputs[100], output[100];
      int c = 0, d = 0;
    
      /* Taking user input */
      printf("Enter some text:\n");
      gets(inputs);
    
      /* Check and remove additional spaces using loop. */
      while (inputs[c] != '\0') {
        if (!(inputs[c] == ' ' && inputs[c + 1] == ' ')) {
          output[d] = inputs[c];
          d++;
        }
        c++;
      }
    
      output[d] = '\0';
      printf("Text after removing extra spaces:\n%s\n", output);
    }

    Program Output:

    W3   S c h o o l s
    Text after removing extra spaces:
    W3 S c h o o l s


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.