C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Strings in C

Remove spaces from string

C Program: Remove All Spaces from a String

C

#include <stdio.h>

 

int main() {

    char str[200], result[200];

    int i, j = 0;

 

    // Input string

    printf("Enter a string: ");

    fgets(str, sizeof(str), stdin);

 

    // Remove spaces

    for (i = 0; str[i] != '\0'; i++) {

        if (str[i] != ' ' && str[i] != '\t') {  // Skip spaces and tabs

            result[j++] = str[i];

        }

    }

 

    result[j] = '\0';  // Null terminate the result string

 

    printf("String after removing spaces: %s", result);

 

    return 0;

}

Output

 
OUTPUT :
Enter a string: IT Developer is Great
String after removing spaces: ITDeveloperisGreat

Explanation

  • The program reads a string using fgets().
  • It checks every character:
    • If the character is not a space or tab, it copies it to the result string.
    • Spaces and tabs are skipped.
  • Finally, it prints the string without spaces.