| | |
Simple array question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 1
Reputation:
Solved Threads: 0
Hiya,
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
Thanks,
- Steve
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

Thanks,
- Steve
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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)
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.
Last edited by Bench; Apr 29th, 2007 at 9:09 am.
¿umop apisdn upside down? >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.
>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.
Last edited by Narue; Apr 29th, 2007 at 9:27 am.
I'm here to prove you 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:
(N - 1) + (N - 2) + ... + 1 bytes.
Here is a supposedly humble implementation which would remove the leading junk character specified by the user:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> void removeJunk(char *string, char junk) { char *p = string; int trimmed = 0; do { if (*string != junk || trimmed) { trimmed = 1; *p++ = *string; } } while (*string++ != '\0'); } int main() { char testStr[] = " such a lonely day "; printf("|%s|", testStr); removeJunk(testStr, ' '); putchar('\n'); printf("|%s|", testStr); getchar(); return 0; }
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Little Simple array question :) (C)
- Simple 2d array help (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Issue creating a vector of a struct in C++
- Next Thread: encrypt text
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






