![]() |
| | #1 |
| Registered User Join Date: Jan 2009
Posts: 37
| I need to split the following string and save each part as a separate variable. Code: "00500+00611" i want to save "00500" as char xcoord, and "00611" as char ycoord. how would i do that? i tired strtok() but i could only extract one digit, here's my code: Code: int main()
{
char Text[80] = {0};
char * line = "GET /send.htm?Text=START+00500+11611 HTTP/1.1";
char *s,*t;
char * xcoord;
char * ycoord;
if(s = strchr(line, '+'))
{
if(t = strchr(s, ' '))
strncpy(Text, s+1, t-s);
}
// at this point Text will be 00500+11611
//xcoord = strtok (Text, "+");
printf("%s\n", xcoord);
//at this point xcoord will be 00500
printf("%s\n", Text);
}
|
| Ali.B is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| Find the address of the plus sign using strchr(). Then, add 1 to that address and that's the address of the 2nd half. You already have the address of the first. memcpy() would work fine.
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #3 |
| Registered User Join Date: Sep 2008 Location: Toronto, Canada
Posts: 507
| Since your xcoord and ycoord are char pointers would you be satisfied with assigning these? Code: xcoord = s + 1; ycoord = xcoord + 6; Code: sscanf(xcoord, "%d", &ixcoord); sscanf(ycoord, "%d", &iycoord); |
| nonoob is offline | |
| | #4 | |
| Registered User Join Date: Jan 2009
Posts: 37
| Quote:
| |
| Ali.B is offline | |
| | #5 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #6 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
Code: char x[] = "00500+00611"; /* 012345 */ char *plus = x + 5; Code: char x[]="00500+00611", *tok1=strtok(x,"+"), *tok2=strtok(NULL,"+");
printf("%s\n%s\n",tok1,tok2);
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 08-11-2009 at 04:02 PM. | |
| MK27 is online now | |
| | #7 |
| Registered User Join Date: Jan 2009
Posts: 37
| |
| Ali.B is offline | |
| | #8 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
You could do the same thing as strtok this way: Code: char x[]="00500+00611", *y=x+6;
x[5]=0;
printf("%s\n%s\n",x,y);
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 08-11-2009 at 04:06 PM. | |
| MK27 is online now | |
| | #9 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
Code: xcoord = Text; /* You may want to do: xcoord[5] = '\0'; so that you don't print the y with the x, but this will change Text as well */ ycoord = Text+6; | |
| tabstop is offline | |
| | #10 |
| Registered User Join Date: Jan 2009
Posts: 37
| yes... problem solved... thanks MK27... i didn't see your first post.... it workd perfectly and i could exctract both parts many thanks |
| Ali.B is offline | |
![]() |
| Tags |
| split, string, strtok |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Passing an array into a function | Ryston | C++ Programming | 4 | 08-29-2006 05:20 AM |
| Access Violation? | rwmarsh | C++ Programming | 6 | 05-04-2006 10:56 AM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| Can someone help me understand this example program | Guti14 | C Programming | 6 | 09-06-2004 12:19 PM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |