0
0
CProgramBeginner · 2 min read

C Program to Remove Spaces from String

Use a loop to copy characters from the original string to a new position skipping spaces with code like for (int i = 0, j = 0; str[i] != '\0'; i++) if (str[i] != ' ') str[j++] = str[i]; str[j] = '\0'; to remove spaces from a string in C.
📋

Examples

Inputhello world
Outputhelloworld
Input c programming
Outputcprogramming
Inputnospace
Outputnospace
🧠

How to Think About It

To remove spaces from a string, think of reading each character one by one. If the character is not a space, keep it. If it is a space, skip it. This way, you build a new string without spaces by overwriting the original string in place.
📐

Algorithm

1
Get the input string.
2
Create two indexes: one for reading characters, one for writing characters.
3
Loop through each character of the string.
4
If the character is not a space, copy it to the write position and move the write index forward.
5
If the character is a space, skip it and do not move the write index.
6
After the loop, add a null character at the write index to end the string.
7
Return or print the modified string.
💻

Code

c
#include <stdio.h>

int main() {
    char str[100];
    int i, j = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i] != '\0'; i++) {
        if (str[i] != ' ' && str[i] != '\n') {
            str[j++] = str[i];
        }
    }
    str[j] = '\0';

    printf("String without spaces: %s\n", str);
    return 0;
}
Output
Enter a string: hello world String without spaces: helloworld
🔍

Dry Run

Let's trace the input "hello world" through the code.

1

Initial string

str = "hello world\n"; i = 0; j = 0

2

Check character 'h'

str[0] = 'h' (not space), copy to str[0], j=1

3

Check character 'e'

str[1] = 'e' (not space), copy to str[1], j=2

4

Check character 'l'

str[2] = 'l' (not space), copy to str[2], j=3

5

Check character 'l'

str[3] = 'l' (not space), copy to str[3], j=4

6

Check character 'o'

str[4] = 'o' (not space), copy to str[4], j=5

7

Check character ' ' (space)

str[5] = ' ' (space), skip, j=5

8

Check character 'w'

str[6] = 'w' (not space), copy to str[5], j=6

9

Check character 'o'

str[7] = 'o' (not space), copy to str[6], j=7

10

Check character 'r'

str[8] = 'r' (not space), copy to str[7], j=8

11

Check character 'l'

str[9] = 'l' (not space), copy to str[8], j=9

12

Check character 'd'

str[10] = 'd' (not space), copy to str[9], j=10

13

Check character '\n'

str[11] = '\n' (newline), skip, j=10

14

End string

Add '\0' at str[10], final string: "helloworld"

i (read index)j (write index)Character readActionString state
00hcopyh
11ecopyhe
22lcopyhel
33lcopyhell
44ocopyhello
55 skiphello
65wcopyhellow
76ocopyhellowo
87rcopyhellowor
98lcopyhelloworl
109dcopyhelloworld
1110\nskiphelloworld
💡

Why This Works

Step 1: Loop through each character

The program reads each character of the string one by one using a loop with index i.

Step 2: Check for spaces

If the character is not a space or newline, it is copied to the position indicated by index j.

Step 3: Overwrite spaces

Spaces are skipped, so the write index j only moves when a non-space character is copied, effectively removing spaces.

Step 4: Terminate the string

After copying, a null character '\0' is added to mark the end of the new string without spaces.

🔄

Alternative Approaches

Using a separate output array
c
#include <stdio.h>

int main() {
    char str[100], result[100];
    int i = 0, j = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    while (str[i] != '\0') {
        if (str[i] != ' ' && str[i] != '\n') {
            result[j++] = str[i];
        }
        i++;
    }
    result[j] = '\0';

    printf("String without spaces: %s\n", result);
    return 0;
}
This method uses extra memory for a new string but keeps the original string unchanged.
Using standard library function to remove spaces (legacy)
c
#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int i = 0, j = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i]; i++) {
        if (str[i] != ' ' && str[i] != '\n') {
            str[j++] = str[i];
        }
    }
    str[j] = '\0';

    printf("String without spaces: %s\n", str);
    return 0;
}
This is similar to the main approach but uses <code>strlen</code> or direct loop condition; it is simple and efficient.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through each character once, so time grows linearly with string length.

Space Complexity

The removal is done in place using the same string, so no extra space is needed beyond variables.

Which Approach is Fastest?

In-place removal is fastest and uses least memory; using a separate array uses more space but keeps original string intact.

ApproachTimeSpaceBest For
In-place removalO(n)O(1)Memory efficient, fast
Separate output arrayO(n)O(n)Preserving original string
Legacy loop with strlenO(n)O(1)Simple and clear code
💡
Use two indexes to overwrite spaces in place without extra memory.
⚠️
Forgetting to add the null character '\0' at the end causes garbage output.