-
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... :(
-
Code:
std::string str = "this is a string";
std::string tmp;
while(str >> tmp)
std::cout << tmp;
-
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;
-
-
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;
}