Sorry for this extremely simple question of mine but...

I'd need this small piece of code that would remove any empty space from the start or the end of a char[]. I've done my basic Java stuff but I'm clueless with c++. So far I've tried to fiddle around with moving the \0 character (which marks the end of an array?) but with very little success. I'd be most delighted for any help :)


- Steve

9 Years
Discussion Span
Last Post by ~s.o.s~

this is a frequently used alogrithm called trimminging -- trim left moves all characters to the left to fill up all the spaces to the left of the string. trim right moves the null-terminator to the left until it reaches the first non-white-sace character (spaces and tabs).

To trim left you need to first find the first non-white-space character in the string. For example " Hello", the first character would be 'H'. then move all the rest of the string to the left so that the result is "Hello". You can use either pointers or indexing with loop counters to do that.


The '\0' character marks the end of a 'C'-Style string. in C++, there is the <string> library which lets you avoid the use of raw character arrays. (the C++ std::string type is not too dissimilar to Java's String type).

Regardless of whether you are using C or C++, moving the '\0' character around isn't the solution. (both languages have library functions for string manipulation)

in the <cctype> header, there is a function called isspace(), which returns non-zero (true) if a character is whitespace - i.e. newline, carriage return, tab, or space.

Think how you would find the first and last occurrances of non-space characters, and save their positions. Once you have these positions, think about how you would calculate the length of the remaining string.

Once you know the starting position of the remaining string, and its length, use the string::substr() method/function
If you are going to carry on using 'C' style strings, then lookup the strncpy() function.


>Regardless of whether you are using C or C++, moving the '\0' character
>around isn't the solution. (both languages have library functions for string manipulation)
For C that statement is debatable. For a full trim it might be better to do the whole operation in one swell foop, if strncpy worked like you seem to think. You still end up tagging '\0' on the end after the copy or you'll get garbage. Worse, a lot of people don't want a full trim, but either a right or a left trim. In that case your suggestion is suboptimal across the board for a right trim and undefined for both because the memory regions overlap. You would have to use memmove to avoid undefined behavior. So it looks like here you're completely wrong. ;)


Moving around chunks of memory using memmove would not be that good an idea. Think of it this way if the string consists of only spaces with just an alphabet at the end. The memmove implementation would move:
(N - 1) + (N - 2) + ... + 1 bytes.

Here is a supposedly humble implementation which would remove the leading junk character specified by the user:

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

void removeJunk(char *string, char junk)
    char *p = string;
    int trimmed = 0;
        if (*string != junk || trimmed)
            trimmed = 1;
            *p++ = *string;
    while (*string++ != '\0');

int main()
    char testStr[] = "      such a lonely day      ";
    printf("|%s|", testStr);
    removeJunk(testStr, ' ');
    printf("|%s|", testStr);
    return 0;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.