Trim a String in C

  1. Use Custom Function to Trim a String in C
  2. Use Another Custom Function to Trim a String in C

This article will explain several methods of how to trim a string in C.

Use Custom Function to Trim a String in C

String trimming functions are not part of the standard C library string utilities or provided by the POSIX compliant functions, so one needs to implement the functionality herself. In this example, we implement the function named trimString that takes a single char pointer argument and returns the pointer to the trimmed string. Note that we, as a caller, are responsible for passing the string that can be modified. In this case, we used strdup to duplicate str1, and when the string is used, it should be freed using the free call.

The trimString function is implemented so that it iterates over the string character by character, and while the given one is a space, it increments the pointer by one. In short, this while loop moves the char pointer point to the first non-space character, trimming the prefix of the string. Next, the incremented pointer is checked if the value is equal to null, implying that the given string had no non-space characters; thus we return the null to denote the error case. The Consequent while loop removes spaces at the end of the string by moving the pointer to the end of the string backward one by one. Once the loop encounters the non-space character, it stops, and the next statement puts the \0 character at the end of it.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

char *trimString(char *str)
{
    char *end;

    while(isspace((unsigned char)*str)) str++;

    if(*str == 0)
        return str;

    end = str + strlen(str) - 1;
    while(end > str && isspace((unsigned char)*end)) end--;

    end[1] = '\0';

    return str;
}

int main(void) {
    const char *str1 = "  temporary string     ";

    printf("%s\n", str1);

    char *tmp = strdup(str1);
    printf("%s\n", trimString(tmp));

    free(tmp);
    exit(EXIT_SUCCESS);
}

Output:

  temporary string     
temporary string

Use Another Custom Function to Trim a String in C

Similar to the previous trimming function, trimString2 is implemented to remove spaces from both sides of the string. The first while loop calculates the number of spaces at the end of the string, and the second loop moves the pointer to the first non-space character. At the end of the loop statements, len contains the number of characters in the trimmed string generated by the strndup function. Notice that the pointer returned from the strndup function should be free by the caller to avoid memory leaks.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

char *trimString2(char *str)
{
    size_t len = strlen(str);

    while(isspace(str[len - 1])) --len;
    while(*str && isspace(*str)) ++str, --len;

    return strndup(str, len);
}

int main(void) {
    char *str2 = "       temporary string  ";

    printf("%s\n", str2);

    char *s = trimString2(str2);
    printf("%s\n", s);

    free(s);
    exit(EXIT_SUCCESS);
}

Output:

     temporary string  
temporary string
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C String

  • Convert a String to Integer in C
  • Use the strdup Function in C