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

</>
Copy
#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:

  1. The function removeSpaces() takes the input string and an output buffer.
  2. We use two indices: i to traverse the input string and j to build the output string.
  3. Each character from input[i] is checked; if it’s not a space, it’s copied to output[j].
  4. After copying all non-space characters, we add a null character ('\0') at the end to terminate the string.
  5. In the main() function, we define an input string and call removeSpaces(), 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

</>
Copy
#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:

  1. The function removeSpacesInPlace() directly modifies the original string.
  2. We use two pointers: i iterates over the original string, and j keeps track of the modified position.
  3. When encountering a non-space character, we overwrite str[j] with str[i].
  4. At the end, we place a null character ('\0') at j to terminate the new string.
  5. 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

</>
Copy
#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:

  1. The function removeSpacesDynamic() dynamically allocates memory to store the modified string.
  2. We allocate memory using malloc() with a size equal to the length of input plus one for the null character.
  3. We iterate through the input string and copy only non-space characters to the allocated memory.
  4. The function returns the newly created string.
  5. In main(), we call removeSpacesDynamic(), print the output, and use free() 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:

  1. Using a separate output array
  2. Modifying the array in place
  3. Using dynamic memory allocation