# Trim function in C

I am trying to write an idiomatic trim function in C. How does this look? Should I instead malloc the new string and return it?

void trim(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (!isspace(input[i])) {
result[j++] = input[i];
}
}
}

-
There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical "trim" function does. Trim removes leading and trailing whitespace. This strips them all out. –  Jeff Mercado Jan 25 '13 at 15:12
Thanks. Can you please elaborate on how to handle buffer overflow attacks? –  jay rulher Jan 25 '13 at 15:44
You should never blindly copy data to some buffer when you don't know how much space is allocated to it, that's just asking for trouble. A simple thing to do would be to add a parameter which takes in the size of the buffer. That way it's all on the caller to tell you how big it really is. Then it's up to you to never attempt to read/write beyond the given length. Of course it's not fool proof, the caller can give you bogus lengths, but that would be a problem on their end, not yours. –  Jeff Mercado Jan 25 '13 at 17:05

As @JeffMercado pointed out, this removes spaces instead of trimming leading and trailing spaces. Assuming you want to keep the current functionality, let's call it remove_spaces.

There's a really subtle bug here:

... isspace(input[i]) ...


isspace takes the value of an unsigned char or EOF. Passing it a char, which is usually signed, will produce undefined behavior. Instead, say:

... isspace((unsigned char) input[i]) ...


Another bug: you don't emit a NUL terminator, meaning the caller would have no way of knowing how long the string is (unless it zeroed out the buffer before calling your function).

Fixing these bugs gives us:

void remove_spaces(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (!isspace((unsigned char) input[i])) {
result[j++] = input[i];
}
}
result[j] = '\0';
}


@JeffMercado also said this function is vulnerable to buffer overflow. In a sense, this is not true, provided the caller knows to allocate a buffer of at least strlen(input) + 1. But the caller might be lazy and just say char result[100]. Adding an output buffer size parameter will likely guard against such a mistake:

void remove_spaces(const char *input, char *output, size_t output_size);


See if you can implement this. Some things to bear in mind:

• Don't forget about the NUL terminator when checking the output buffer size.

• Don't be like strncpy and omit the NUL terminator when you have to truncate the string, as it can lead to subtle bugs.

• If you use int for i and j and size_t for output_size, you should get compiler warnings about comparison between signed and unsigned. If you don't, turn up your compiler warnings. If you're using GCC from the command line, get in the habit of typing gcc -Wall -W.

-

Have you given thought to more of an in-place approach? Here's a quick example (I took one shortcut by taking strlen, which means an extra string traversal):

void trim(char *input)
{
char *dst = input, *src = input;
char *end;

// Skip whitespace at front...
//
while (isspace((unsigned char)*src))
{
++src;
}

// Trim at end...
//
end = src + strlen(src) - 1;
while (end > src && isspace((unsigned char)*end))
{
*end-- = 0;
}

// Move if needed.
//
if (src != dst)
{
while ((*dst++ = *src++));
}
}

-

We know that we can move a pointer forward and backward, and we also know that we can trim a string from the left. If we increment the pointer and decrement the pointer to trim from the right, then two while loops are enough. You'll notice that the right walk count is less than the left walk count.

Right-trim code:

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

void trim_both(char *, char *);

int main (void) {
char title[100] = "                 My         long          string     ";
char title_t[100] = "";

(void) printf("String before left trim is:[%s]\n", title);
trim_both(title, title_t);
(void) printf("String after left trim is:[%s]\n", title_t);

}

// trim spaces from left
void trim_both(char *title_p, char *title_tp) {
int flag = 0;

// from left
while(*title_p) {
if(!isspace((unsigned char) *title_p) && flag == 0) {
*title_tp++ = *title_p;
flag = 1;
}
title_p++;
if(flag == 1) {
*title_tp++ = *title_p;
}
}

// from right
while(1) {
title_tp--;
if(!isspace((unsigned char) *title_tp) && flag == 0) {
break;
}
flag = 0;
*title_tp = '\0';
}
}

-

Easiest way (removes only spaces):

Trim.Start:

1. Compare chars until they equal ' ' (space or other chars like \n or \t) at the string start and increment temp (i) variable.
2. Move pointer about i (str+=i). Now string starts from char which is not a space char (or any other white char).

Trim.End:

1. Do the same what for Trim.Start but from the end of the string.
2. Set last char (last space) as \0.

The important thing is that function takes pointer to pointer (string). Watch for the function call: StringTrim(&p2);

char * StringTrim(char * *pointerToString)
{
u8 start=0, length=0;

// Trim.Start:
length = strlen(*pointerToString);
while ((*pointerToString)[start]==' ') start++;
(*pointerToString) += start;

if (start < length) // Required for empty (ex. "    ") input
{
// Trim.End:
u8 end = strlen(*pointerToString)-1; // Get string length again (after Trim.Start)
while ((*pointerToString)[end]==' ') end--;
(*pointerToString)[end+1] = 0;
}

return *pointerToString;
}


Usage:

char str1[] = " test1   ";
char * p1 = str1;

Debug("1. before trim: [%s]", p1);
StringTrim(&p1);
Debug("1. after trim [%s]", p1);

char str2[] = "   test2";
char * p2 = str2;

Debug("2. before trim: [%s]", p2);
StringTrim(&p2);
Debug("2. after trim [%s]", p2);

char str3[] = "test3   ";
char * p3 = str3;

Debug("3. before trim: [%s]", p3);
StringTrim(&p3);
Debug("3. after trim [%s]", p3);

char str4[] = "    ";
char * p4 = str4;

Debug("4. before trim: [%s]", p4);
StringTrim(&p4);
Debug("4. after trim [%s]", p4);

char str5[] = "";
char * p5 = str5;

Debug("5. before trim: [%s]", p5);
StringTrim(&p5);
Debug("5. after trim [%s]", p5);


Result:

1. before trim: [ test1   ]
1. after trim [test1]
2. before trim: [   test2]
2. after trim [test2]
3. before trim: [test3   ]
3. after trim [test3]
4. before trim: [    ]
4. after trim []
5. before trim: []
5. after trim []

-