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. :)
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).
Try to learn advance topics: Automata, is a good way to parse something.
Well, what I need it to do is automatically pick out the string that follows the symbol I am searching for.
Originally Posted by MacGyver
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? :)
Traditional parsing example:
while((c = getchar()) != EOF)
if(c == '$')
while(isalpha(c = getchar()))
Output:Is this help?
hello my name is $bob what is yours? hi! my name is $angela, nice to meet you