C Program to Remove Spaces from String
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
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace the input "hello world" through the code.
Initial string
str = "hello world\n"; i = 0; j = 0
Check character 'h'
str[0] = 'h' (not space), copy to str[0], j=1
Check character 'e'
str[1] = 'e' (not space), copy to str[1], j=2
Check character 'l'
str[2] = 'l' (not space), copy to str[2], j=3
Check character 'l'
str[3] = 'l' (not space), copy to str[3], j=4
Check character 'o'
str[4] = 'o' (not space), copy to str[4], j=5
Check character ' ' (space)
str[5] = ' ' (space), skip, j=5
Check character 'w'
str[6] = 'w' (not space), copy to str[5], j=6
Check character 'o'
str[7] = 'o' (not space), copy to str[6], j=7
Check character 'r'
str[8] = 'r' (not space), copy to str[7], j=8
Check character 'l'
str[9] = 'l' (not space), copy to str[8], j=9
Check character 'd'
str[10] = 'd' (not space), copy to str[9], j=10
Check character '\n'
str[11] = '\n' (newline), skip, j=10
End string
Add '\0' at str[10], final string: "helloworld"
| i (read index) | j (write index) | Character read | Action | String state |
|---|---|---|---|---|
| 0 | 0 | h | copy | h |
| 1 | 1 | e | copy | he |
| 2 | 2 | l | copy | hel |
| 3 | 3 | l | copy | hell |
| 4 | 4 | o | copy | hello |
| 5 | 5 | skip | hello | |
| 6 | 5 | w | copy | hellow |
| 7 | 6 | o | copy | hellowo |
| 8 | 7 | r | copy | hellowor |
| 9 | 8 | l | copy | helloworl |
| 10 | 9 | d | copy | helloworld |
| 11 | 10 | \n | skip | helloworld |
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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| In-place removal | O(n) | O(1) | Memory efficient, fast |
| Separate output array | O(n) | O(n) | Preserving original string |
| Legacy loop with strlen | O(n) | O(1) | Simple and clear code |
'\0' at the end causes garbage output.