C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-05-2008, 12:06 PM   #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.
CM_2020 is offline   Reply With Quote
Old 07-05-2008, 12:24 PM   #2
Deathray Engineer
 
MacGyver's Avatar
 
Join Date: Mar 2007
Posts: 3,211
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).
__________________
MacGyver is offline   Reply With Quote
Old 07-05-2008, 01:07 PM   #3
Ugly C Lover
 
audinue's Avatar
 
Join Date: Jun 2008
Location: Indonesia
Posts: 462
Try to learn advance topics: Automata, is a good way to parse something.
audinue is offline   Reply With Quote
Old 07-08-2008, 01:52 AM   #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?
CM_2020 is offline   Reply With Quote
Old 07-08-2008, 02:17 AM   #5
Ugly C Lover
 
audinue's Avatar
 
Join Date: Jun 2008
Location: Indonesia
Posts: 462
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?
audinue is offline   Reply With Quote
Old 07-08-2008, 10:33 AM   #6
Registered User
 
Join Date: Jul 2008
Posts: 3
Yep. Thanks.
CM_2020 is offline   Reply With Quote
Reply

Tags
c code, parsing, symbol

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:22 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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