Getting strings from a string

This is a discussion on Getting strings from a string within the C++ Programming forums, part of the General Programming Boards category; I just installed Ubuntu and these 2-3 days I haven't been able to log in here. I had a thread ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    43

    Getting strings from a string

    I just installed Ubuntu and these 2-3 days I haven't been able to log in here. I had a thread opened here. The problem is solved now, so forgive me for not telling about it.

    What I want to do now is to get strings from a string. If I have the following string:

    " this + that - anotherOne + again ",

    then I am going to have a string called "this", another one called "that", and so on, without the spaces. here is my code:
    Code:
    vector<string> siruriCitite;
    
    void f(string sir)
    {
        int pointer = 0;
        int length = sir.length();
        int found = 0;
        int firstLetter, lastLetter;
        int dimension = 0;
        do
        {
            firstLetter = sir.find_first_not_of(" ", found + 1);
            lastLetter = sir.find_last_not_of(" ", found + 1);
            for(pointer = found; pointer < length; pointer++)
            {
                if(sir[pointer] == '+' || sir[pointer] == '-' || sir[pointer] == '/')
                {
                    found = pointer;
                    break;
                }
            }
            siruriCitite.resize(dimension + 1);
            siruriCitite[dimension] = sir.substr(firstLetter, lastLetter);
            dimension++;
        }
        while(pointer < length);
    }
    After running in main : f("CradleofFilth + TheCruxshadows - Ubuntu / SoporAeternus") and then
    Code:
         int l = siruriCitite.size();
        for(int i = 0; i < l; l++)
        {
            cout << siruriCitite[l] << endl;
        }
    , nothing happens. It's like an infinite loop. Also, the operating system almost crashes.. Thank you in advance for your time ..
    Last edited by Veneficvs; 05-05-2010 at 08:43 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's probably because you have an infinite loop. pointer can never make it past the first +-/ character in the string, since it gets reset every time.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by tabstop View Post
    That's probably because you have an infinite loop. pointer can never make it past the first +-/ character in the string, since it gets reset every time.
    Why does it get reset? It is always initialised with the value of found. And found is always growing when founding a + , a - or a /.

    Sorry, I forgot to translate it the first time. "Gasit" means "Found" in my language. I edited it now.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In that case, you are still finding the same +-/ character each time, since pointer will immediately be the right character since it hasn't moved since the last time; you need to start looking after found

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A simpler way for tokenizing on whitespace would be to use a stream.

    Code:
    std::stringstream ss(input);
    std::string token;
    while (ss >> token) {
        //throw away operators if needed
        results.push_back(token);
    }
    Also it is a very bad idea to resize the vector manually. You are just giving up the (amortized) constant time insertions at the end that the vector offers, instead requiring all insertions to have linear complexity. Plus it is more error prone.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Thank you very much... I totally forgot about the stringstream stuff. And you were right. I modified the loop by writing "found + 1". It gave me a Segmentation Error , then asked me to press ENTER to exit, so at least no infinite loop.

    But how do I eliminate the + , - , / from the input?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Using the stringstream approach? Check to see if your token is +, -, or /; if it is, do nothing.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by tabstop View Post
    Using the stringstream approach? Check to see if your token is +, -, or /; if it is, do nothing.
    I got it now. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Help! Homework using Strings
    By wco5002 in forum C++ Programming
    Replies: 16
    Last Post: 09-27-2007, 03:53 AM
  3. Strings - char mystring[n] vs. string mystring
    By Diablo84 in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2005, 05:54 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

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