You can use std::stringstream
and std::getline
.
I also suggest that you use std::vector
as it's resizeable.
In the example below, we get input line and store it into a std::string
, then we create a std::stringstream
to hold that data. And you can use std::getline
with ;
as delimiter to store the string data between the semicolon into the variable word
as seen below, each "word" which is pushed back into a vector:
int main()
{
string line;
string word;
getline(cin, line);
stringstream ss(line);
vector<string> vec;
while (getline(ss, word, ';')) {
vec.emplace_back(word);
}
for (auto i : vec) // Use regular for loop if you can't use c++11/14
cout << i << '\n';
Alternatively, if you can't use std::vector
:
string arr[256];
int count = 0;
while (getline(ss, word, ';') && count < 256) {
arr[count++] = word;
}
Live demo
Outputs:
man,meal,moon
fat,food,feel
cat,coat,cook
love,leg,lunch