C Program to remove spaces from a string


In this program, You will learn how to remove spaces from a string in c.


J o h n S m i t h

After remove spaces: JohnSmith


Example: How to remove spaces from a string in c

Run it Now
#include<stdio.h>
#include<string.h>

int main() {

    char str[100];
    int i, j = 0, k, size;

    printf("Enter String :");
    fgets(str, 100, stdin);

    size = strlen(str);

    for (i = 0; i < size; i++) {
        if (str[i] != ' ') {
            str[j] = str[i];
            j++;
        }
    }
    
    str[j] = '\0';
    printf("\nAfter Remove Spaces :%s", str);

    return 0;
}

Output:

Enter String :b b c
After Remove Spaces :bbc