Simple string manipulation

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Simple string manipulation

 
0
  #1
Jan 9th, 2007
Hi, I am trying to make a method inside a class that will remove the first 2 characters of a string of characters. Sounds simple enough, I though it was, but actually implementing it took a bit of thought and doesn't seem to work.

  1. void classname::removeFirstChars(char * buffer)
  2. {
  3. char actualMessage[sizeof(buffer)-2];
  4. int a=0;
  5.  
  6. // loop through starting from third character of buffer and copy char
  7. // by char to actualMessage. (i.e. a=0, a=1, start at a=2)
  8. while (strlen(buffer) > a)
  9. {
  10. if ( a > 1 )
  11. {
  12. actualMessage[a-2] = buffer[a];
  13. }
  14. a++;
  15. }
  16.  
  17. // after doing this i have tried concattenating a null character '\0' to the
  18. // end but this doesn't seem to work either
  19.  
  20. if (buffer[0] == 'm')
  21. {
  22. MessageBox(0,actualMessage, inet_ntoa(_incoming.sin_addr),0);
  23. }
  24. else if (buffer[0] == 's')
  25. {
  26. system(actualMessage);
  27. }
  28. buffer[0]='\0';
  29. actualMessage[0]='\0';
  30. }

As you can probably see this code is to take a message in the format of
s#message here or m#message here and then take the actual message part and deal with it accordingly either by putting it into a message box or using it as a system command. Any help?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,965
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Simple string manipulation

 
0
  #2
Jan 9th, 2007
First of all: Why are you using char[] with C++? You should use strings.
This line :char actualMessage[sizeof(buffer)-2]; is causing some troubles.
Since you have shown effort in making it yourself:

  1.  
  2. void cremoveFirstChars(char * buffer)
  3. {
  4. char* actualMessage = new char[sizeof(buffer)];
  5. for (int a = 0; a <=strlen(buffer) - 1; a++)
  6. actualMessage[a] = buffer[a+2];
  7. actualMessage[strlen(buffer) -1]= '\0';
  8. std::cout << actualMessage;
  9. }
This is what should do the trick I think, I didn't test it but I should work..

regards Niek
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Simple string manipulation

 
0
  #3
Jan 9th, 2007
yea thanks, that works fine, i've just been overthinking things and its a while since i've worked with C++.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Simple string manipulation

 
0
  #4
Jan 9th, 2007
How about a slight modification that uses the the knowledge that c-strings are always terminated with \0:
  1.  
  2. void cremoveFirstChars(char * buffer)
  3. {
  4. char* actualMessage = new char[sizeof(buffer)];
  5. p = -1; // initialize the pointer
  6. do
  7. {
  8. p++; // next character
  9. actualMessage[p] = buffer[p+2];
  10. } while (!actualMessage[p]); // stop after the \0 is moved
  11. std::cout << actualMessage;
  12. }

And niek_e, please watch your intenting -- make your code readable. Teach by example.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Simple string manipulation

 
0
  #5
Jan 9th, 2007
Just noticing a few things (using WaltP's code as a reference):
1) using sizeof(buffer) will not give you the right length since buffer is a pointer, so it'll probably be the same size as an int. You don't want actualMessage to be 4 chars long (assuming an int is 4 bytes). You should use strlen(buffer) rather than sizeof(buffer).
2) memory allocated needs to be deleted.

[edit:] here's an example of what I mean in part 1:
  1. #include <stdio.h>
  2.  
  3. void foo(char* c)
  4. {
  5. printf("size: %d\n", sizeof(c));
  6. }
  7.  
  8. int main()
  9. {
  10. char array[128] = {0};
  11. printf("size: %d\n", sizeof(array));
  12. foo(array);
  13. return 0;
  14. }
output:
  1. $ ./a.out
  2. size: 128
  3. size: 4
[edit 2:] Also noticed: the OPs method of allocating a dynamic array on the stack is only supported by a couple compilers and isn't very portable. Probably want to use new/delete (or malloc/free) instead, as the other replies have done.
Last edited by Infarction; Jan 9th, 2007 at 4:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,965
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Simple string manipulation

 
0
  #6
Jan 10th, 2007
Originally Posted by WaltP
And niek_e, please watch your intenting -- make your code readable. Teach by example
Tss.. By now you should know that I always use indenting, but I missed it just this once. What kind of programmer do you think I am?

Originally Posted by Infarction
memory allocated needs to be deleted.
You're absolutely right, I made the snippet form the top of my head and I just forgot it.

Regards Niek
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Simple string manipulation

 
0
  #7
Jan 10th, 2007
Originally Posted by niek_e View Post
Tss.. By now you should know that I always use indenting, but I missed it just this once.
Sorry, but I look at hundreds of posts a day. I don't memorize every individual's style and if I see something wrong, I sometimes point it out. It was just your turn
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC