C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-12-2009, 04:08 AM   #1
Registered User
 
Join Date: Jan 2009
Posts: 37
Post make one string from 2 strings.

hello
i managed to parse the parts that i need from this string:
Code:
"GET /send.htm?Text=START+00500+11611 HTTP/1.1"
with this pair of code ( with help of this forum ):
Code:
char Text[80] = {0};
	char * line = "GET /send.htm?Text=START+00500+11611 HTTP/1.1";
	char *s,*t;
	char bt_msg[20] = {0};

	if(s = strchr(line, '+')) 
	{
		if(t = strchr(s, ' '))
			strncpy(Text, s+1, t-s);
	}


	char *xcoord=strtok(Text,"+"), *ycoord=strtok(NULL,"+");
	printf("x-coord:%s\n", xcoord);
	printf("y-coord:%s\n", ycoord);
}
so basically:
the output will be:
Code:
x-coord: 00500
y-coord: 11611
i need to assemble a payload that follows the following structure:
Code:
"X=00500Y=11611"
than means the first five digits represent the x-coord and the 2nd five digits represent y-coord.
i need to save assemble this payload from those coordinates and put in a variable so i can use it later on.may be save it as bt_msg. how could i do it?
thanks

Last edited by Ali.B; 08-12-2009 at 04:30 AM.
Ali.B is offline   Reply With Quote
Old 08-12-2009, 07:39 AM   #2
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
sprintf() is probably what you want to use, or, you can use a series of strcat() functions.
__________________
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-12-2009, 07:44 AM   #3
Registered User
 
Join Date: Jan 2009
Posts: 37
could u show me how with a piece of code? with both functions; sprintf() and strcat();
Ali.B is offline   Reply With Quote
Old 08-12-2009, 07:47 AM   #4
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
Code:
char a[] = "A" ; 
char b[] = "B" ; 
char c[] = "C" ; 
char abc[10]  = {0} ; 

strcat(abc, a) ; 
strcat(abc, b) ; 
strcat(abc, c) ; 

printf("variable abc = %s\n", abc) ;
__________________
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-12-2009, 08:21 AM   #5
Registered User
 
Join Date: Jan 2009
Posts: 37
yes i understood this part... and I can use it now... thanks for the sample code
the output will be this; since my variables are xcoord and ycoord;
0050011611
now how can i add other characters to this string? and make it look like this:
X=00500Y=11611
X,= and Y should be added to the string.. how can i handle this?
Ali.B is offline   Reply With Quote
Old 08-12-2009, 08:25 AM   #6
Registered User
 
Join Date: Jan 2009
Posts: 37
Post

Quote:
Originally Posted by Ali.B View Post
yes i understood this part... and I can use it now... thanks for the sample code
the output will be this; since my variables are xcoord and ycoord;
0050011611
now how can i add other characters to this string? and make it look like this:
X=00500Y=11611
X,= and Y should be added to the string.. how can i handle this?
nvm... i figured it out by playing the peice of code u Dino gave me
i did like this:
Code:
int main()
{
	char Text[80] = {0};
	char * line = "GET /send.htm?Text=START+00500+11611 HTTP/1.1";
	char *s,*t;
	char bt_msg[20] = {0};

	if(s = strchr(line, '+')) 
	{
		if(t = strchr(s, ' '))
			strncpy(Text, s+1, t-s);
	}

	char *xcoord=strtok(Text,"+"), *ycoord=strtok(NULL,"+");
	printf("x-coord:%s\n", xcoord);
	printf("y-coord:%s\n", ycoord);
	
	strcat(bt_msg, "X=");
	strcat(bt_msg, xcoord);
	strcat(bt_msg, "Y="); 
	strcat(bt_msg, ycoord); 
		
	printf("bt_msg= %s\n", bt_msg) ;

	
}
and the output is as i wanted:
Code:
bt_msg= X=00500Y=11611
thanks Dino for ur help
Ali.B is offline   Reply With Quote
Old 08-12-2009, 08:43 AM   #7
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
Glad you figured it out. That's what it's all about - you learning through experimentation and trial and error.
__________________
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-12-2009, 10:04 AM   #8
Registered User
 
Join Date: Jan 2009
Posts: 37
Exactly....
Ali.B is offline   Reply With Quote
Reply

Tags
string

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Inheritance Hierarchy] User Input on program with constructors. How ? chandreu C++ Programming 8 04-25-2008 02:45 PM
Help! Homework using Strings wco5002 C++ Programming 16 09-27-2007 03:53 AM
Calculator + LinkedList maro009 C++ Programming 20 05-17-2005 12:56 PM
Classes inheretance problem... NANO C++ Programming 12 12-09-2002 03:23 PM
"Operator must be a member function..." (Error) Magos C++ Programming 16 10-28-2002 02:54 PM


All times are GMT -6. The time now is 07:07 AM.


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