Simple array question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 1
Reputation: steve_d is an unknown quantity at this point 
Solved Threads: 0
steve_d steve_d is offline Offline
Newbie Poster

Simple array question

 
0
  #1
Apr 29th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Simple array question

 
0
  #2
Apr 29th, 2007
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Simple array question

 
0
  #3
Apr 29th, 2007
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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Simple array question

 
0
  #4
Apr 29th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Simple array question

 
0
  #5
Apr 29th, 2007
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. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Simple array question

 
0
  #6
Apr 29th, 2007
Your algorithm doesn't work either with strings that contain nothing but junk characters. It will do nothing in that case.
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Simple array question

 
0
  #7
Apr 29th, 2007
Think again -- its a do while loop working with C strings (they have a null terminator at the end).
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC