C Programming

How to Remove Whitespace from Strings with strtrim() in C Programming

Whitespace is an essential part of text data, but when it comes to processing text data, whitespace can become more of a challenge. Whitespace characters like spaces, tabs, and new lines can make it difficult to compare and work with strings. These characters can cause errors and false results in text processing. To avoid these issues, we can remove whitespace from strings using the strtrim() function in C.

What is strtrim() Function

The C computer language comes with a built-in function called strtrim(). Whitespace characters at the start and termination of a string are removed. The string to be trimmed is the only input given to the function. The original string is not changed; instead, a new string is produced by removing the whitespace. The new string is returned by the function.

The strtrim() function is defined in the <string.h> header file in C. The strtrim() function removes leading and trailing white spaces from the string. We can use this function in our program to make sure that our strings are trimmed of all whitespaces.

How to Remove Whitespace from Strings with strtrim() in C Programming

The syntax for the strtrim() function in C is defined is as follows:

char *strtrim(char *str)

The function returns a pointer to a new string that has the whitespace removed. The argument str is the string to trim. The original string is not modified. The new string returned by the function must be freed when it is no longer needed, to prevent memory leaks.

To begin, the strtrim() function counts the number of whitespace characters at the string’s beginning. The amount of whitespace characters at the string’s end is then counted. The non-whitespace characters are then inserted between the two sets of whitespace characters to produce a new string.

Here is an example of using the strtrim() function:

#include <stdio.h>

#include <string.h>

#include <ctype.h>

char* strtrim(char* str) {

size_t len = strlen(str);

    if (len == 0) {

      return str;

    }

size_t start = 0;

    while (isspace(str[start])) {

      start++;

    }

size_t end = len - 1;

    while (isspace(str[end])) {

      end--;

    }

size_t i;

    for (i = 0; i <= end - start; i++) {

      str[i] = str[start + i];

    }

    str[i] = '\0';

    return str;

}

int main() {

    char str[] = " Linux hint ";

printf("Before trimming: "%s"\n", str);

strtrim(str);

printf("After trimming: "%s"\n", str);

    return 0;

}

The strtrim() function removes any starting or ending whitespace from a string when given a string as input (such as blank spaces, tabs, newlines, etc.). After applying in-place modifications to the input string, the function returns a pointer to the updated text. In the main function, we define the str string, which has leading and trailing whitespace. After executing the strtrim() function on str, the original and trimmed strings are then printed using printf().

Output

Conclusion

Processing text data can be quite difficult when dealing with whitespace. Yet, we can rapidly and effectively remove whitespace from strings using the C strtrim() function. The function is simple to use and is compatible with most programs that process strings. Without having to manually remove whitespace characters, it makes it simple to edit and comp returns.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.