Remove White Spaces from a Character Array in C
To remove white spaces from a character array in C, we iterate through the array and copy only non-space characters to a new array or modify the original array in place. This ensures that all spaces are eliminated while maintaining the original sequence of non-space characters.
Examples of Removing White Spaces
1. Removing White Spaces Using a Separate Output Array
In this example, we will traverse the input string and copy only non-space characters to a separate output array. The output array will store the modified string without white spaces.
main.c
#include <stdio.h>
void removeSpaces(const char *input, char *output) {
int i = 0, j = 0;
while (input[i] != '\0') {
if (input[i] != ' ') {
output[j++] = input[i];
}
i++;
}
output[j] = '\0'; // Null terminate the modified string
}
int main() {
char input[] = "Hello World C Program";
char output[50]; // Sufficient size to store the modified string
removeSpaces(input, output);
printf("Original: \"%s\"\n", input);
printf("Without Spaces: \"%s\"\n", output);
return 0;
}
Explanation:
- The function
removeSpaces()
takes the input string and an output buffer. - We use two indices:
i
to traverse the input string andj
to build the output string. - Each character from
input[i]
is checked; if it’s not a space, it’s copied tooutput[j]
. - After copying all non-space characters, we add a null character (
'\0'
) at the end to terminate the string. - In the
main()
function, we define an input string and callremoveSpaces()
, then print the modified string.
Output:
Original: "Hello World C Program"
Without Spaces: "HelloWorldCProgram"
2. Removing White Spaces In-Place
In this example, we will modify the character array in place without using an additional array, thus optimizing memory usage.
main.c
#include <stdio.h>
void removeSpacesInPlace(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (str[i] != ' ') {
str[j++] = str[i];
}
i++;
}
str[j] = '\0'; // Null terminate the modified string
}
int main() {
char str[] = "C Programming Language";
removeSpacesInPlace(str);
printf("Without Spaces: \"%s\"\n", str);
return 0;
}
Explanation:
- The function
removeSpacesInPlace()
directly modifies the original string. - We use two pointers:
i
iterates over the original string, andj
keeps track of the modified position. - When encountering a non-space character, we overwrite
str[j]
withstr[i]
. - At the end, we place a null character (
'\0'
) atj
to terminate the new string. - In the
main()
function, we pass the character array and print the modified string.
Output:
Without Spaces: "CProgrammingLanguage"
3. Removing White Spaces Using Dynamic Memory Allocation
In this example, we dynamically allocate memory to store the modified string without white spaces. This is useful when working with strings of unknown length.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* removeSpacesDynamic(const char *input) {
int i = 0, j = 0;
char *output = (char*)malloc(strlen(input) + 1);
if (output == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
while (input[i]) {
if (input[i] != ' ') {
output[j++] = input[i];
}
i++;
}
output[j] = '\0'; // Null terminate the new string
return output;
}
int main() {
char input[] = "Apple Banana Cherry";
char *output = removeSpacesDynamic(input);
printf("Without Spaces: \"%s\"\n", output);
free(output); // Free dynamically allocated memory
return 0;
}
Explanation:
- The function
removeSpacesDynamic()
dynamically allocates memory to store the modified string. - We allocate memory using
malloc()
with a size equal to the length ofinput
plus one for the null character. - We iterate through the
input
string and copy only non-space characters to the allocated memory. - The function returns the newly created string.
- In
main()
, we callremoveSpacesDynamic()
, print the output, and usefree()
to release allocated memory.
Output:
Without Spaces: "AppleBananaCherry"
Conclusion
In this tutorial, we explored different ways to remove white spaces from a character array in C:
- Using a separate output array
- Modifying the array in place
- Using dynamic memory allocation