![]() |
| | #1 |
| Registered User Join Date: Jul 2008
Posts: 3
| C parsing 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 | |
| | #2 |
| Deathray Engineer 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 | |
| | #3 |
| Ugly C Lover Join Date: Jun 2008 Location: Indonesia
Posts: 462
| Try to learn advance topics: Automata, is a good way to parse something. |
| audinue is offline | |
| | #4 | |
| Registered User Join Date: Jul 2008
Posts: 3
| Quote:
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 | |
| | #5 |
| Ugly C Lover 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');
}
}
Code: hello my name is $bob what is yours? hi! my name is $angela, nice to meet you Code: bob angela |
| audinue is offline | |
| | #6 |
| Registered User Join Date: Jul 2008
Posts: 3
| Yep. Thanks. |
| CM_2020 is offline | |
![]() |
| Tags |
| c code, parsing, symbol |
| Thread Tools | |
| Display Modes | |
|
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 |