Thread: trimming strings...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    trimming strings...

    hi ppl...sorry for asking this again but im having a horrible time trying to prune this string...the string in 'word' could be anything like below: (ive put quotes so you know that there are spaces...otherwise there are no quotes in the string 'word'
    Code:
    '*CLASS* alt yes no '
    '*CLASS* alt hello right'
    i use getline to read in a line into word...now i need to extract only the words from the string excluding all spaces or newlines etc...

    someone told me about using find() and then substr() but i don't know the size of the words...can be 3 letters or 2 or anything...
    could someone show me how to use it...this is the code i have so far:
    Code:
    ifstream in(filename);
    in >> word;                 //just to pick up the word *CLASS*
    //then i check if the word is *CLASS*
    //if it is then:
    in.getline(word);         //this gets the whole line after *CLASS*
    
    //now i want to extract one word at a time and then do some operation on it
    //each word i extract should not have any spaces in it...
    while           //what should be the condition in while?
    {
       word.substr(1,word.find(' '))
       //then use the word
    }
    Regards,

    Farooq

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use of a stringstream object would seem easiest:

    Code:
    #include <sstream>
    #include <string>
    ...
    std::string word = "*CLASS* alt hello right";
    std::string temp;
    std::istringstream parser(word);
    while( parser >> temp )
        std::cout << temp << std::endl;
    Should output something like:
    Code:
    *CLASS*
    alt
    hello
    right
    All neatly seperated out according to the spaces in the original string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    thanks for the reply...
    what if your string was like this:
    Code:
    std::string word = "*CLASS* alt hello right ";
    i.e. not ending with a newline but with a space and then newline...
    or maybe with more than one space between words?

    i would like to use your methods because it seems a lot more simpler than playing around with substrings...

    Regards,

    Farooq

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Got your PM...

    I had a test program set up to run my little program sample above and make sure it actually did what I was saying it did. I just modified it to put lots of spaces between the individual words and also at the end of the string. After I reran the test program I got the same results... so it would seem you can use the example provided as is with no changes and it will still do what you want.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    thanks a lot...ill get on it right away...

    do you know what's the proper way to copy the string from word to a static char array...

    this doesn't work for me:
    Code:
    static char classes[MAX_CLASSES][MAX_STRING];
    classes[0] = word;           //error on this line
    error is: E2277 Lvalue required
    should i use strcpy?

    it works if i just do: in >> classes[0];
    btw word is defined as: char word[MAX_STRING];

    Regards,

    Farooq
    Last edited by alvifarooq; 11-05-2004 at 12:15 PM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ya i got it...i just use strcpy()

    thanks...

    Farooq

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    another thing...what if the string had commas in between them? like
    Code:
    std::string word = "*CLASS*,alt,hello,right ";
    Regards,

    Farooq

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    i just checked it and it doesn't work for commas...treats them like one word...there's another line in my file in which words are only separated only by commas...i have that working using getline(word, MAX_STRING, ',') and reading in word by word...but the problem is when i want to check the end of the line...right now i do this:
    Code:
    while (in.peek() != '\n')
    {
       getline(word,MAX_STRING, ',')
       //process it
    }
    this is not too good because the file might contain at the end of a line a space and then a newline character...
    so what's the best way to check that...is there anyway to modify the parser so it can have the commas as the delimiter?

    Regards,

    Farooq

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can use the replace function to change all commas to spaces and just run the same algorithm:

    Code:
    #include <sstream>
    #include <string>
    #include <algorithm>
    ...
    std::string word = "*CLASS*,alt,hello,right ";
    std::string temp;
    std::replace(word.begin(),word.end(),',',' ');
    std::istringstream parser(word);
    while( parser >> temp )
        std::cout << temp << std::endl;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    many thanks!

    Farooq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. Trimming Strings
    By shiju in forum C Programming
    Replies: 6
    Last Post: 01-20-2004, 08:47 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM