Hi there,
I have a problem in breaking a sentence into each string. i have tried to use find() to search for the " "(space), use substr to search from the first string index into the find space number. It works for the first string. But I am unable to start find the " "(space) for the second one. It makes me cant get the second string.
If there is anyone know how to solve it, please let me know. Thanks in advance.
This is my code
#include <cstring>
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main()
{
string *command = new string[5];
string input;
//for ex.enter my name is need_Direction
cout << "enter something:";
getline(cin, input);
cout << input.size() << endl;
cout << "user_input:" << input << endl;
int pos = input.find(" ");
int currPos;
int pos2 = input.find(" ");
if(pos != string::npos)
{
cout << "found!" << endl;
command[0] = input.substr(0, pos);
cout << "first string : " << command[0] << endl << endl;
currPos = pos;
//continue from current position and find the second " "
if(pos != string::npos)
{
cout << "found again!" << endl;
cout << pos << endl;
command[1] = input.substr(currPos+1, pos);
cout << "second string : " << command[1] << endl;
}
}
else
cout << "string not found" << endl;
return 0;
}