Substing in VC++ [modified]
-
Hi all, I have this string, that would look something like this: ZA123654PCNAME I want to break the string apart like this: ZA123654 (Please note that this piece is not constant) PCNAME I have tried to get the secondnd part (by doing the following: below), but how can I get the first part, this is if I'm doing it correctly..
if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0)
Thanx in advance -- modified at 8:57 Thursday 9th November, 2006The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Hi all, I have this string, that would look something like this: ZA123654PCNAME I want to break the string apart like this: ZA123654 (Please note that this piece is not constant) PCNAME I have tried to get the secondnd part (by doing the following: below), but how can I get the first part, this is if I'm doing it correctly..
if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0)
Thanx in advance -- modified at 8:57 Thursday 9th November, 2006The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
On which 'parameters' do you split the string ? I mean, how do you know which is the first part of the string and which is the last part of the string ? Are all the strings the same size ? If not, is there a delimiter character ? Or what ? By looking at your code, it seems that the last part is always PCNAME and is always 6 char long. So, you can then compute the lenght of the first part:
int Len = strlen(Request) - 6;
Then, you can copy up to Len characters of your string in a buffer:strncpy(szBuff,Request,Len);
If that's not the case, please provide more information about how you want to split the string.
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
On which 'parameters' do you split the string ? I mean, how do you know which is the first part of the string and which is the last part of the string ? Are all the strings the same size ? If not, is there a delimiter character ? Or what ? By looking at your code, it seems that the last part is always PCNAME and is always 6 char long. So, you can then compute the lenght of the first part:
int Len = strlen(Request) - 6;
Then, you can copy up to Len characters of your string in a buffer:strncpy(szBuff,Request,Len);
If that's not the case, please provide more information about how you want to split the string.
Cédric Moonen Software developer
Charting control [Updated - v1.1]My bad Cédric ... I forgot to mention that there is a space between the first part and the PCNAME. And yes PCNAME will always be a constant. But thank you so much, I really appreciate the help (code provided works). Regards
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Hi all, I have this string, that would look something like this: ZA123654PCNAME I want to break the string apart like this: ZA123654 (Please note that this piece is not constant) PCNAME I have tried to get the secondnd part (by doing the following: below), but how can I get the first part, this is if I'm doing it correctly..
if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0)
Thanx in advance -- modified at 8:57 Thursday 9th November, 2006The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
« Programm3r » wrote:
if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0)
In this context, both
strstr()
andstrncmp()
are doing redundant work. Ifstrstr()
returns a non-NULL
pointer, it means that "PCNAME" was found inRequest
. Therefore, there's no need to follow that with a call tostrncmp()
. Similarly, ifstrstr()
returns aNULL
pointer,strncpy()
will fail. Make sense?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
My bad Cédric ... I forgot to mention that there is a space between the first part and the PCNAME. And yes PCNAME will always be a constant. But thank you so much, I really appreciate the help (code provided works). Regards
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Just for fun and if your interrested in that solution, try this :
char string[] = "abc def ghi"; char * token = strtok(string, " "); while( token ) { printf("[%s]\n", token); token = strtok(NULL, " "); }
Thanx XtremDev ... I'll check it out ... :)
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
« Programm3r » wrote:
if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0)
In this context, both
strstr()
andstrncmp()
are doing redundant work. Ifstrstr()
returns a non-NULL
pointer, it means that "PCNAME" was found inRequest
. Therefore, there's no need to follow that with a call tostrncmp()
. Similarly, ifstrstr()
returns aNULL
pointer,strncpy()
will fail. Make sense?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
David ... your to clever bro... :) .. but to answer your question ... It makes sense ... kinda :)
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r