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

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

    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

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    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. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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);
    Last edited by MK27; 08-11-2009 at 04:02 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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

    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?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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);
    Last edited by MK27; 08-11-2009 at 04:06 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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;

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    yes... problem solved...
    thanks MK27... i didn't see your first post.... it workd perfectly and i could exctract both parts
    many thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array into a function
    By Ryston in forum C++ Programming
    Replies: 4
    Last Post: 08-29-2006, 05:20 AM
  2. Access Violation?
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2006, 10:56 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread