Thread: How to analyze a string for numbers or other characters

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    How to analyze a string for numbers or other characters

    I am trying to write a program that can do postfix expressions in c++. First I have to read in the user input as a string. But how would I break up the string so I can tell whether each data is a number or a math operator? Please help me with this.

    Code:
    int main()
    {
    string a;
    getline(cin, a)
    //How to break up the string into individual chunks for analyzing?
    return 0;
    }

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by chickenlittle View Post
    I am trying to write a program that can do postfix expressions in c++. First I have to read in the user input as a string. But how would I break up the string so I can tell whether each data is a number or a math operator? Please help me with this.

    Code:
    int main()
    {
    string a;
    getline(cin, a)
    //How to break up the string into individual chunks for analyzing?
    return 0;
    }
    Think of the problem you are trying to solve: You have a string of n elements, for each element you want to check whether it matches any one of these [0,1,2,3,4,5,6,7,8,9] or if not, whether it matches anyone of these [+, -, *, /].

    You would probably want to put the code for this into a function, and you should probably take into account what the function should return, and especially what it should return if it runs into a string element who doesn't match any of the above (Like a whitespace character or a letter or whatever). I would suggest calling the function isNum(), then you could return true if it is a match to the first list of characters, if not, return false if it is a match to the second list of characters, and possibly throw an exception if an invalid element was found.

    Give it a try and show us what you come up with.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    But how would you check to see if the string contains numbers or math operators? That is where I am stuck in. Would we use stringstream?

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by chickenlittle View Post
    But how would you check to see if the string contains numbers or math operators? That is where I am stuck in. Would we use stringstream?
    I tried to write out the problem in terms that could be almost directly translated to C++:

    You have a string of n elements, for each element you want to check if it equals any one of these [0,1,2,3,4,5,6,7,8,9] or else, if it equals anyone of these [+, -, *, /]

    You see now? This is probably as close to writing out the program logic in pseudocode, as possible, and that wouldn't teach you anything at all. Now, write some code. If it doesn't work, show it to us.
    Last edited by Neo1; 10-01-2011 at 07:22 PM.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    It would be more worthwhile to check if it's an operator, and if not, assume it's a number.

    Code:
    //I'll assume you have a vector<string> named origin that contians all your strings.
    for(int i=0; i<origin.size(); i++)
    {
    switch(origin[i][0])//run tests to see the FIRST CHARACTER of the string
    {
    case 'x':
    //do something
    break;
    //run all your operators in such a way...
    default:
    //this is gonna trigger if it was NOT an operator, do something with the string then
    break;
    }
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Shingetsu Kurai View Post
    It would be more worthwhile to check if it's an operator, and if not, assume it's a number.
    Ok, good so far....

    Quote Originally Posted by Shingetsu Kurai View Post
    I'll assume you have a vector<string> named origin that contians all your strings.
    Why? Let's keep things on task for the OP shall we. He specifically stated he had a string, not a vector of strings.

    @OP: The std::string can be manipulated char by char by iterating through it:
    Code:
    for(int i =0; i<str.size();i++){
         std::cout<<str[i]<<std::endl;
    }
    Just simply apply your shunting yard algorithm to each char of the string.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting numbers to characters?
    By Aslan14 in forum C Programming
    Replies: 3
    Last Post: 12-14-2010, 03:22 PM
  2. Best way to read characters, strings and numbers
    By VelvetMirror in forum C Programming
    Replies: 3
    Last Post: 05-16-2010, 07:16 PM
  3. seeding with numbers, generating with characters?
    By muzihc in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 11:50 PM
  4. Numbers printed with characters
    By bnkslo in forum C Programming
    Replies: 15
    Last Post: 05-08-2008, 07:07 AM
  5. How would I only enter numbers and not characters
    By chrismax2 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2004, 03:19 PM