0

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

1
  • Here's a simple way to figure out how to do this, and it never fails to work. Just take out a blank sheet of paper. Write down using brief, simple sentences in plain English, a step-by-step process of doing this. When done, call your rubber duck for an appointment. We don't write entire programs for other people, on Stackoverflow. We always refer such questions to your rubber duck. After your rubber duck approves your proposed plan of action, simply take what you've written down and translate it directly into C++. Mission accomplished! Commented Jul 19, 2020 at 14:45

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.