Aug 31, 2012 at 6:43am UTC
I have sentence like " I am King" in a string or character array.
I want this string to be placed in a vector<string> object.
Like a -> I
a -> king
How do I do that ,,
I have tried this method but it doesn't work :
int length1 = strlen(ch1);
for(int i = 0;i<length1;i++)
for(int j = i;j < length1;j++)
if(ch1[j] == ' ')
for(int c = i;c <= j;c++)
Aug 31, 2012 at 6:55am UTC
Firstly, use code tags.
Secondly, you need to get the input into a string, preferably not a char
Thirdly, all you need to do is find ' ' characters and their location, and use them as slice indices. So:
"I am king"
Spaces at: 2 and 5
So then all chars between: 1; 3+4; 6-END
are your words you need to push.
Aug 31, 2012 at 7:06am UTC
But how can we trace element at each and every position if we use strings?
Aug 31, 2012 at 10:11am UTC
I have solved this program finally , Thank you very much for your support.
using namespace std;
cout<<"Please enter something :"<<endl;
int length = strlen(word);
// copy the sentence into a vector
if(word[i] == ' ' || i == length - 1 )
for(it = a.begin();it <a.end();it++)