string help

This is a discussion on string help within the C Programming forums, part of the General Programming Boards category; New to this forum. I need help on how to extract certain variables from a string. For example, if i ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    string help

    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

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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...

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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