KingAzaiez
KingAzaiez

Reputation: 113

Split string into vector

For example, any character different than a b c..x y z or A B C..X Y Z or - needs to be seperated and put in a vector.

How can I achieve this ?

std::string inputLine;
vector<string> inputs;
getline(std::cin, inputLine);

at this point I got the string from the user input, how can I split it ?

For example: hello,sir my nameéis ada-m should be put in a vector as follows

inputs.at(0): hello

inputs.at(1): sir

inputs.at(2): my

inputs.at(3): name

inputs.at(4): is

inputs.at(5): ada-m

Upvotes: 0

Views: 417

Answers (1)

Thomas Sablik
Thomas Sablik

Reputation: 16448

This is an simple algorithm

  • Create an empty vector of strings inputs
  • Create an empty string s
  • Iterate over each char c of your input string inputLine
    • If c in 'a' 'b' 'c'..'x' 'y' 'z' or 'A' 'B' 'C'..'X' 'Y' 'Z' or '-'
      • Append c to s
    • Else
      • Append s to inputs (you could also check if s is empty)
      • Clear s
  • Append last string s to inputs (you could also check if s is empty)

Upvotes: 1

Related Questions