C Program to remove spaces from a string


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


X i i t h

After remove spaces: Xiith

Example: How to remove spaces from a string in c.

#include<stdio.h>
#include<string.h>

int main() {

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

    printf("Enter String :");
    scanf("%[^\n]%*c", str); 

    size = strlen(str);

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

    return 0;
}

Output:

Enter String :Xi i th 34
After Remove Spaces :Xiith34