How to Trim a String in C

Jinku Hu Feb 02, 2024
  1. Use Custom Function to Trim a String in C
  2. Use Standard Library Functions to Trim a String in C
  3. Use the strtok() Function to Trim a String in C
  4. Conclusion
How to Trim a String in C

In programming, the need to trim strings (removing unwanted whitespace characters from the beginning and end) often arises. While C doesn’t provide a built-in function for this task, this article presents multiple methods for trimming strings.

We’ll explore how to create a custom function, utilize standard library functions, and employ the strtok() function. Each method offers its unique approach to achieving this common string manipulation task.

Use Custom Function to Trim a String in C

While C doesn’t provide a built-in function for trimming strings, you can create your custom function to achieve this task. Before we dive into writing the custom function, let’s understand the problem at hand.

Trimming a string means eliminating any whitespace characters from the beginning and end of the string while preserving the characters in between. Whitespace characters include space, tab (\t), newline (\n), carriage return (\r), and other similar characters.

To create a custom function for trimming a string in C, we’ll follow these steps:

  • Define a function that takes a string (a character array) as an argument.
  • Determine the starting and ending positions of the trimmed string by iterating through the input string.
  • Remove any leading whitespace characters.
  • Remove any trailing whitespace characters.
  • Modify the original string in place to reflect the trimmed string.

Here’s a C code example that demonstrates this process:

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

void trimString(char *str) {
  int start = 0, end = strlen(str) - 1;

  // Remove leading whitespace
  while (isspace(str[start])) {
    start++;
  }

  // Remove trailing whitespace
  while (end > start && isspace(str[end])) {
    end--;
  }

  // If the string was trimmed, adjust the null terminator
  if (start > 0 || end < (strlen(str) - 1)) {
    memmove(str, str + start, end - start + 1);
    str[end - start + 1] = '\0';
  }
}

int main() {
  char text[] = "   Trim this string   ";
  trimString(text);
  printf("Trimmed: '%s'\n", text);
  return 0;
}

As seen in the code above, the trimString function takes a string (char *str) as its input.

Then, we initialize two integer variables, start and end. start is set to 0, and end is calculated as the length of the input string minus one (strlen(str) - 1).

We use a loop to remove leading whitespace characters. The loop checks if the character at the start position is a whitespace character using isspace.

If it is, we increment start to move past the leading whitespace.

Another loop is used to remove trailing whitespace characters. Starting from the end of the string, we move backward (decrementing end) while the character at the end position is a whitespace character.

This ensures that trailing whitespace is also removed.

After removing the leading and trailing whitespace, we check if any trimming occurred. If either start moved from its initial position (indicating leading whitespace removal) or end is not at the end of the string (indicating trailing whitespace removal), we modify the original string.

Lastly, we use memmove to copy the trimmed part of the string to the beginning, and we place a null terminator ('\0') at the right position to correctly terminate the trimmed string.

To verify that the custom trimString function works as expected, we call it in the main function and provide it with a test string. In this example, we use the string " Trim this string " for testing.

After trimming, the output will be:

Trimmed: 'Trim this string'

The custom trimString function effectively removed the leading and trailing whitespace, resulting in the expected trimmed string.

Use Standard Library Functions to Trim a String in C

Trimming strings can also be achieved efficiently using standard library functions. Two standard library functions, strspn and strcspn, can be used to trim a string.

  1. strspn Function: This function calculates the length of the initial segment of the string that consists of the characters specified in the second argument. We can utilize it to find the length of the leading whitespace characters.

  2. strcspn Function: This function calculates the length of the initial segment of the string that does not contain any of the characters specified in the second argument. We can use it to find the position of the first non-whitespace character from the end of the string.

Here’s how you can use these functions to trim a string:

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

void trimString(char *str) {
  char *start =
      str +
      strspn(str, " \t\n\r");  // Points to the first non-whitespace character
  char *end =
      str + strlen(str) - 1;  // Points to the last character of the string

  while (end > start &&
         (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) {
    --end;  // Move the end pointer backward while it points to whitespace
  }

  *(end + 1) = '\0';  // Place the null terminator after the last non-whitespace
                      // character

  // Move the trimmed string to the start of the buffer
  if (start > str) {
    memmove(str, start,
            end - start +
                2);  // +2 to include the last character and null terminator
  }
}

int main() {
  char text[] = "   Trim this string   ";
  trimString(text);
  printf("Trimmed: '%s'\n", text);
  return 0;
}

The trimString function is designed to remove leading and trailing whitespace from a given string. Here’s a step-by-step breakdown of the corrected function:

  1. Identify the Start: We begin by locating the first non-whitespace character. The strspn function is perfect for this task—it scans str for whitespace characters and returns the number of characters that are whitespace. By adding this number to str, we set the start pointer to the first character that isn’t whitespace.
  2. Find the End: We want end to point to the last non-whitespace character in str. To get there, we set end to point at the last character in the string (strlen(str) - 1). Then we step backwards, decrementing end, as long as it points to a whitespace character. We stop as soon as end points to a non-whitespace character.
  3. Terminate the String: With end in the correct position, we place a null terminator right after the last non-whitespace character by setting *(end + 1) to '\0'. This effectively cuts off any trailing whitespace.
  4. Shift the String (if needed): If the string had leading whitespace, start will be greater than str. In this case, we need to shift the non-whitespace part of the string to the beginning of the buffer. We do this with memmove, which handles overlapping memory areas. We move end - start + 2 characters to include the entire trimmed string and the new null terminator.

In main, we test trimString with a string that has extra spaces before and after the words “Trim this string”. Once the function is called, those extra spaces should be gone, and the printf statement will display the neat and trimmed string.

With these corrections, running the program will correctly output: Trimmed: 'Trim this string'. The function now does what it’s supposed to—trim the string from both ends.

Trimmed: 'Trim this string'

Use the strtok() Function to Trim a String in C

The strtok() function in C is primarily used for string tokenization, breaking down a string into smaller parts called tokens. While it’s not directly designed for trimming strings, with a specific approach, strtok() can efficiently achieve this task.

The strtok() function breaks a string into tokens based on a delimiter. To trim a string, we can use the whitespace characters as delimiters and reconstruct the trimmed string.

It has the following syntax:

char *strtok(char *str, const char *delimiters);

Parameters:

  • str: A pointer to the null-terminated string to be tokenized.
  • delimiters: A string containing delimiter characters. strtok() will use these delimiters to split the input string into tokens.

The function returns a pointer to the next token in the input string. Subsequent calls to strtok() with a NULL pointer as the first argument will continue to return subsequent tokens from the original string.

Here’s an example of how strtok() can be employed for string trimming:

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

void trimString(char *str) {
  char *token;
  char temp[1024];  // Temporary buffer to store the trimmed version, size
                    // should be enough to hold the original string
  int firstToken = 1;

  // Initialize the temporary buffer
  temp[0] = '\0';  // Set the first character to the null terminator to create
                   // an empty string

  // Tokenize the string by whitespace characters
  token = strtok(str, " \t\n\r");
  while (token != NULL) {
    if (firstToken) {
      // Copy the first token to the temp buffer
      strcpy(temp, token);
      firstToken = 0;
    } else {
      // Concatenate subsequent tokens with a space
      strcat(temp, " ");
      strcat(temp, token);
    }
    token = strtok(NULL, " \t\n\r");
  }

  // Copy the trimmed string back to the original string
  strcpy(str, temp);
}

int main() {
  char text[] = "   Trim this string   ";
  trimString(text);
  printf("Trimmed: '%s'\n", text);
  return 0;
}

The trimString function in the modified code aims to trim leading and trailing whitespace from a string using the strtok function. We define a buffer temp large enough to hold the modified string without leading and trailing spaces.

Inside the function, we declare a character pointer token which will point to each token found by strtok. We also initialize a temporary buffer temp as an empty string by setting the first character to the null terminator.

The strtok function is then called with the input string and a list of whitespace characters as delimiters. It returns the first token found in the string. We enter a while loop to process each token found by strtok.

We introduce a flag firstToken to check if the current token is the first one. For the first token, we copy it directly to temp. For subsequent tokens, we append a single space to temp before appending the token. This is to ensure that words are separated by a single space in the trimmed string.

After processing all tokens, we exit the loop with temp containing the concatenated tokens, which is the string stripped of leading and trailing whitespace and with only single spaces between words.

Finally, we copy temp back to the input string str using strcpy. This overwrites the original string with its trimmed version.

In the main function, we demonstrate the use of trimString. We define a string text with extra whitespace, call trimString to trim it, and then print the result, which should be the string 'Trim this string' without the leading or trailing whitespace.

The output will be:

Trimmed: 'Trim this string'

Conclusion

In this article, you’ve learned three distinct methods to trim strings in C. Whether you prefer a custom function for full control, standard library functions for efficiency, or the creative use of strtok() for string tokenization, you can now confidently manipulate your strings to eliminate unwanted whitespace.

These techniques allow you to efficiently handle string trimming tasks in your C programs, ensuring your strings are clean and ready for further processing.

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C String