delimiting string

This is a discussion on delimiting string within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a string array. In this string array for example if i get teststring[0] out it returns me ...

  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
    21,457
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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