Remove Leading and Trailing Spaces from a String in C

In C, we can remove leading and trailing spaces from a string by implementing custom trimming functions. Since the standard C library does not provide a built-in function for trimming spaces, we achieve this using character manipulation techniques, including pointer operations and string functions like strlen(), isspace(), and memmove().

In this tutorial, we will explore different ways to remove spaces efficiently with examples.


Examples to Remove Leading and Trailing Spaces

1. Removing Leading Spaces Using Pointer Manipulation

In this example, we will create a function that removes leading spaces by shifting the starting position of the string until a non-space character is found.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>

void trimLeadingSpaces(char *str) {
    int i = 0;
    while (isspace((unsigned char)str[i])) {
        i++; // Move past leading spaces
    }
    if (i > 0) {
        int j = 0;
        while (str[i]) {
            str[j++] = str[i++]; // Shift characters left
        }
        str[j] = '\0'; // Null-terminate the string
    }
}

int main() {
    char str[] = "    Hello, World!";
    trimLeadingSpaces(str);
    printf("Trimmed String: '%s'\n", str);
    return 0;
}

Explanation:

  1. We define the function trimLeadingSpaces(), which takes a string as input.
  2. We use isspace() to check and skip leading spaces.
  3. Once a non-space character is found, we shift the characters to the left using a while loop.
  4. The modified string is null-terminated to ensure correctness.

Output:

Trimmed String: 'Hello, World!'

2. Removing Trailing Spaces Using strlen()

In this example, we remove trailing spaces by finding the last non-space character and placing a null terminator after it.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void trimTrailingSpaces(char *str) {
    int length = strlen(str);
    while (length > 0 && isspace((unsigned char)str[length - 1])) {
        length--; // Move back over trailing spaces
    }
    str[length] = '\0'; // Null-terminate the string
}

int main() {
    char str[] = "Hello, World!    ";
    trimTrailingSpaces(str);
    printf("Trimmed String: '%s'\n", str);
    return 0;
}

Explanation:

  1. The function trimTrailingSpaces() finds the length of the input string using strlen().
  2. It moves backward from the end of the string, skipping spaces using isspace().
  3. Once a non-space character is found, a null terminator is placed after it.

Output:

Trimmed String: 'Hello, World!'

3. Removing Both Leading and Trailing Spaces

In this example, we combine both leading and trailing trimming functions to remove spaces from both ends of a string.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void trimSpaces(char *str) {
    // Remove leading spaces
    int i = 0;
    while (isspace((unsigned char)str[i])) i++;
    
    int j = 0;
    while (str[i]) str[j++] = str[i++];
    str[j] = '\0';

    // Remove trailing spaces
    int length = strlen(str);
    while (length > 0 && isspace((unsigned char)str[length - 1])) length--;
    str[length] = '\0';
}

int main() {
    char str[] = "   Hello, World!   ";
    trimSpaces(str);
    printf("Trimmed String: '%s'\n", str);
    return 0;
}

Explanation:

  1. The function trimSpaces() calls the leading space removal logic first.
  2. We shift the characters left until the first non-space character appears.
  3. Next, we apply the trailing space removal logic by adjusting the null terminator.

Output:

Trimmed String: 'Hello, World!'

Conclusion

In this tutorial, we explored different ways to remove leading and trailing spaces from a string in C:

  1. Pointer manipulation: Efficiently removes leading spaces.
  2. Using strlen(): Used to remove trailing spaces.
  3. Combining both approaches: Completely trims the string.