Thread: delimiting string

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    6

    Question delimiting string

    Hi,

    I have a string array. In this string array for example if i get teststring[0] out it returns me "this is a test string".

    with this string its delimited by white spaces. But i am having trouble to delimit them into "this" "is" "a" "test" "string".

    i tried using the following codes but char mystring[a] does not accept in the string returned from teststring[0].
    Code:
    string a = teststring[0];
    char mystring[] = a;
    char *delim = " "; // only blank
    char *token;
    
    token = strtok(mystring, delim);
    
    while (token !=NULL)
    {
    //compare ... (strcmp (token,"and") )
    //continue loop if equal
    string s = token;
    token = strtok(NULL,delim);
    cout << "the string token is " << s<<endl;
    }
    Is there any other better way to delimit the strings in my string array?? please help beeen figuring this for long but cant seem to find a solution...

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    std::string str = "this is a string";
    std::string tmp;
    while(str >> tmp)
        std::cout << tmp;

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    i tried the codes but it cant work? any suggestion what the error might be due to?

    Code:
        string str = "this is a string";
        string tmp;
        while(str >> tmp)
        cout << tmp;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Is "operator>>" defined for string objects? I doubt it.

    If you want to use the ">>" operator, try:
    Code:
    #include <sstream>
    #include <iostream>
    
    int main()
    {
      std::istringstream str("This is a string");
      std::string tmp;
      while(str >> tmp)
        std::cout << tmp << std::endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 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, 06:49 PM