C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-11-2009, 02:29 PM   #1
Registered User
 
Join Date: Jan 2009
Posts: 37
Post 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);
}
thanking you in advance
Ali.B is offline   Reply With Quote
Old 08-11-2009, 02:39 PM   #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   Reply With Quote
Old 08-11-2009, 02:43 PM   #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;
... 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.
nonoob is offline   Reply With Quote
Old 08-11-2009, 03:19 PM   #4
Registered User
 
Join Date: Jan 2009
Posts: 37
Quote:
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?
Ali.B is offline   Reply With Quote
Old 08-11-2009, 03:28 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by Ali.B View Post
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.
tabstop is offline   Reply With Quote
Old 08-11-2009, 03:57 PM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
Originally Posted by Ali.B View Post
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);
__________________

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   Reply With Quote
Old 08-11-2009, 04:02 PM   #7
Registered User
 
Join Date: Jan 2009
Posts: 37
Post

Quote:
Originally Posted by tabstop View Post
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?
Ali.B is offline   Reply With Quote
Old 08-11-2009, 04:03 PM   #8
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
Originally Posted by Ali.B View Post
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);
__________________

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   Reply With Quote
Old 08-11-2009, 04:05 PM   #9
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by Ali.B View Post
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;
tabstop is offline   Reply With Quote
Old 08-11-2009, 04:08 PM   #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   Reply With Quote
Reply

Tags
split, string, strtok

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:25 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22