Thread: C parsing

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    C parsing

    So, here's what I'm wondering if you guys can help me with: I'm fairly new to coding C and I might just be missing a function, so if so this should be easy.

    Here's the problem, or, at least, what I need my program to do.
    User enters a string, optionally containing a symbol. So, like, "$." I need my program to parse out the string that directly follows it (excluding whitespace) and store it in a char.

    So, if "user" entered "hello, my name is $bob what is yours?", the program would return "bob" to a character.

    If this post is redundant, I apologize- I looked and didn't find anything similar.

    I would greatly appreciate any help.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    First step in finding the '$' is using strchr(). The next step depends upon what you want the program to do. You could manually search through the rest of the string char by char until you find a character to stop at. Finally, after knowing the start and end of the substring, you could strcpy() it to a new malloc()'ed buffer and do whatever you want with it (remembering to free() the buffer later on).

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Try to learn advance topics: Automata, is a good way to parse something.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Quote Originally Posted by MacGyver View Post
    First step in finding the '$' is using strchr(). The next step depends upon what you want the program to do. You could manually search through the rest of the string char by char until you find a character to stop at. Finally, after knowing the start and end of the substring, you could strcpy() it to a new malloc()'ed buffer and do whatever you want with it (remembering to free() the buffer later on).
    Well, what I need it to do is automatically pick out the string that follows the symbol I am searching for.

    Now.... I think I may have something here, and I was wondering if you guys could help me a little further? What my newest idea is was to use strtok() to split up input into a series of tokens using the ' ' character. Sooo... "hello, my name is $bob what is yours" would be split up into each of the individual words. However, in what way can I later search each individual token for the "$" character? Also, if I am going about this in the completely wrong way, please- someone tell me?

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Traditional parsing example:
    Code:
    char c;
    
    while((c = getchar()) != EOF)
    {
        if(c == '$')
        {
            while(isalpha(c = getchar()))
            {
                putchar(c);
            }
    
            putchar('\n');
        }
    }
    Input:
    Code:
    hello my name is $bob what is yours? hi! my name is $angela, nice to meet you
    Output:
    Code:
    bob
    angela
    Is this help?

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Yep. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need sth about parsing
    By Masterx in forum C++ Programming
    Replies: 6
    Last Post: 11-07-2008, 12:55 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. draw tree graph of yacc parsing
    By talz13 in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 01:33 AM
  4. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 05:31 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM

Tags for this Thread