make one string from 2 strings.

This is a discussion on make one string from 2 strings. within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    39

    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.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    could u show me how with a piece of code? with both functions; sprintf() and strcat();

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    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?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    39

    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

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    Exactly....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Help! Homework using Strings
    By wco5002 in forum C++ Programming
    Replies: 16
    Last Post: 09-27-2007, 03:53 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM

Tags for this Thread


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