Thread: make one string from 2 strings.

  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
    Chappell Hill, Texas
    Posts
    2,332
    sprintf() is probably what you want to use, or, you can use a series of strcat() functions.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    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) ;
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    Glad you figured it out. That's what it's all about - you learning through experimentation and trial and error.
    Mainframe assembler programmer by trade. C coder when I can.

  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, 03:23 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM

Tags for this Thread