943,931 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3589
  • C++ RSS
Apr 29th, 2007
0

Simple array question

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
steve_d is offline Offline
1 posts
since Apr 2007
Apr 29th, 2007
0

Re: Simple array question

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 29th, 2007
0

Re: Simple array question

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.
Last edited by Bench; Apr 29th, 2007 at 9:09 am.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Apr 29th, 2007
0

Re: Simple array question

>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.
Last edited by Narue; Apr 29th, 2007 at 9:27 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 29th, 2007
0

Re: Simple array question

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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void removeJunk(char *string, char junk)
  5. {
  6. char *p = string;
  7. int trimmed = 0;
  8. do
  9. {
  10. if (*string != junk || trimmed)
  11. {
  12. trimmed = 1;
  13. *p++ = *string;
  14. }
  15. }
  16. while (*string++ != '\0');
  17. }
  18.  
  19. int main()
  20. {
  21. char testStr[] = " such a lonely day ";
  22. printf("|%s|", testStr);
  23. removeJunk(testStr, ' ');
  24. putchar('\n');
  25. printf("|%s|", testStr);
  26. getchar();
  27. return 0;
  28. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Apr 29th, 2007
0

Re: Simple array question

Your algorithm doesn't work either with strings that contain nothing but junk characters. It will do nothing in that case.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 29th, 2007
0

Re: Simple array question

Think again -- its a do while loop working with C strings (they have a null terminator at the end).
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Issue creating a vector of a struct in C++
Next Thread in C++ Forum Timeline: encrypt text





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC