New to this forum. I need help on how to extract certain variables from a string. For example, if i have this equation "tan (2x+3)=5", how can I separate the "tan", '2','x', "+3" and '5' and assign them into separate variables? Thanks
Printable View
New to this forum. I need help on how to extract certain variables from a string. For example, if i have this equation "tan (2x+3)=5", how can I separate the "tan", '2','x', "+3" and '5' and assign them into separate variables? Thanks
Look up the strtok() function. You supply a string, and a list of delimiters (characters that may separate the "tokens" in your strings), and each subsequent call to strtok() will return another pointer to the next token.
Your application might be more specific however, in which case you'll need to write a parser. Simply put a parser is simply a program that reads through the text, character by character, and interprets it's meaning and gives it a structure.
edit: for instance, if you read in the word "tan" or "sin", you'd know that's a function. You'd also know that anything in the parentheses following that was a parameter for that function. So you could then take the contents of the parentheses, and put everything but spaces in an array, and then you would know that each element is an operand, followed by an operator, followed by another operand, possibly follower by an operator, etc...
Thanks for replying to my post Sean.I'm really new to C, so i do not know how to get around doing this parser. Is there a fixed structure for this parser?