Thread: Parse A Part of String

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

    Post Parse A Part of String

    Hello Guys...
    i'm very new to C Programming, but i'm learning quite well...
    how can i parse only a part of a string?
    my string is always like this:
    Code:
    GET /send.htm?Text=START HTTP/1.1
    only the "START" part can change according to user input, but that part is always after the "=" sign and before " " (the space) before HTTP.
    i need to retrieve that part only and save into an variable. how can i do this?
    i would appreciate it if some one could help me with this, an example code or something would be really nice.

    thanking you in advance

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Use strchr() to get the location of the '='. Then use strchr() again to get the location of the space after the equals sign. Finally use strncpy() to copy out the characters between those 2 locations.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    can u give an example? I've never worked with those functions.... or a link that provides some examples.
    thanks

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Ali.B View Post
    can u give an example? I've never worked with those functions.... or a link that provides some examples.
    thanks
    cstring (string.h) - C++ Reference

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    I looked at those examples, but I'm still lost... can someone post a piece of code that could demonstrate the use of strchr() and strncpy()?

  7. #7
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Code:
    	char Text[80] = {0};
    	char * line = "GET /send.htm?Text=START HTTP/1.1";
    
    	char *s,*t;
    
    	if(s = strchr(line, '=')) 
    	{
    		if(t = strchr(s, ' '))
    			strncpy(Text, s+1, t-s);
    	}
    	printf("Text = %s\n", Text);

  8. #8
    Banned
    Join Date
    Aug 2009
    Posts
    43
    there is much easyer way
    if str="123456"
    sscanf(str,"%3d",&num);

    num will be 123

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    Thanks a million Scarlet7, now I understand how to use it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. String Parse.
    By Coder87C in forum C# Programming
    Replies: 3
    Last Post: 08-16-2005, 02:18 AM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM

Tags for this Thread