# Thread: Split a string into two parts and save as two variables

1. ## Split a string into two parts and save as two variables

hello guys...

I need to split the following string and save each part as a separate variable.
Code:
`"00500+00611"`
there string always will have 5 digits, a Plus (+) sign in the middle and 5 digits on the other side. so its always following the same structure.

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);
}```

2. 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.

3. Since your xcoord and ycoord are char pointers would you be satisfied with assigning these?

Code:
```xcoord = s + 1;
ycoord = xcoord + 6;```
... probably not. You want the numbers converted to integers?

Code:
```sscanf(xcoord, "%d", &ixcoord);
sscanf(ycoord, "%d", &iycoord);```
Something like that. I haven't tested.

4. xcoord = s + 1;
ycoord = xcoord + 6;
this didn' work for me , I don't need to convert those to integers, just save them as two char variables. how would i do that?

5. Originally Posted by Ali.B
this didn' work for me , I don't need to convert those to integers, just save them as two char variables. how would i do that?
You can't store a string as a char variable, because a char variable can only hold one char, not six as needed here.

6. Originally Posted by Ali.B
this didn' work for me , I don't need to convert those to integers, just save them as two char variables. how would i do that?
This was not intend to convert them to integers, it's pointer arithmetic:

Code:
```char x[] = "00500+00611";
/*          012345   */
char *plus = x + 5;```
To extract the second digit with strtok:
Code:
```char x[]="00500+00611", *tok1=strtok(x,"+"), *tok2=strtok(NULL,"+");
printf("%s\n%s\n",tok1,tok2);```

7. Originally Posted by tabstop
You can't store a string as a char variable, because a char variable can only hold one char, not six as needed here.
right, i meant to store is as string, like "00611" as one string and "00500" as another string... how could i do that?

8. Originally Posted by Ali.B
right, i meant to store is as string, like "00611" as one string and "00500" as another string... how could i do that?
You can use strtok, you just did not do it properly, I presume (see my last post).

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);```

9. Originally Posted by Ali.B
right, i meant to store is as string, like "00611" as one string and "00500" as another string... how could i do that?
Probably as
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;```

10. yes... problem solved...
thanks MK27... i didn't see your first post.... it workd perfectly and i could exctract both parts
many thanks