| | |
Simple string manipulation
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
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.
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?
C Syntax (Toggle Plain Text)
void classname::removeFirstChars(char * buffer) { char actualMessage[sizeof(buffer)-2]; int a=0; // loop through starting from third character of buffer and copy char // by char to actualMessage. (i.e. a=0, a=1, start at a=2) while (strlen(buffer) > a) { if ( a > 1 ) { actualMessage[a-2] = buffer[a]; } a++; } // after doing this i have tried concattenating a null character '\0' to the // end but this doesn't seem to work either if (buffer[0] == 'm') { MessageBox(0,actualMessage, inet_ntoa(_incoming.sin_addr),0); } else if (buffer[0] == 's') { system(actualMessage); } buffer[0]='\0'; actualMessage[0]='\0'; }
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?
First of all: Why are you using char[] with C++? You should use strings.
This line :
Since you have shown effort in making it yourself:
This is what should do the trick I think, I didn't test it but I should work..
regards Niek
This line :
char actualMessage[sizeof(buffer)-2]; is causing some troubles. Since you have shown effort in making it yourself:
C Syntax (Toggle Plain Text)
void cremoveFirstChars(char * buffer) { char* actualMessage = new char[sizeof(buffer)]; for (int a = 0; a <=strlen(buffer) - 1; a++) actualMessage[a] = buffer[a+2]; actualMessage[strlen(buffer) -1]= '\0'; std::cout << actualMessage; }
regards Niek
How about a slight modification that uses the the knowledge that c-strings are always terminated with \0:
And niek_e, please watch your intenting -- make your code readable. Teach by example.
C Syntax (Toggle Plain Text)
void cremoveFirstChars(char * buffer) { char* actualMessage = new char[sizeof(buffer)]; p = -1; // initialize the pointer do { p++; // next character actualMessage[p] = buffer[p+2]; } while (!actualMessage[p]); // stop after the \0 is moved std::cout << actualMessage; }
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Just noticing a few things (using WaltP's code as a reference):
1) using
2) memory allocated needs to be deleted.
[edit:] here's an example of what I mean in part 1:
output:
[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.
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> void foo(char* c) { printf("size: %d\n", sizeof(c)); } int main() { char array[128] = {0}; printf("size: %d\n", sizeof(array)); foo(array); return 0; }
C Syntax (Toggle Plain Text)
$ ./a.out size: 128 size: 4
Last edited by Infarction; Jan 9th, 2007 at 4:33 pm.
•
•
•
•
Originally Posted by WaltP
And niek_e, please watch your intenting -- make your code readable. Teach by example
•
•
•
•
Originally Posted by Infarction
memory allocated needs to be deleted.
Regards Niek
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Memory reallocation
- Next Thread: help:stl vector of user defined objects
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windowsapi






