C Programming Examples Tutorial Index

C String Programs

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