c++ - How to parse a user's input and store each individual character as a string -
c++ - How to parse a user's input and store each individual character as a string -
okay i'm working calculator programme takes in user input(ex. "(3+(4+12))"), , need parse user's input , store in array of strings having problem doing so. code this
void parseinput(string input) { vector <string> input_vector; for(int = 0; < input.size(); ++i) { if(isdigit(input.at(i)) == 0 && isdigit(input.at(i + 1)) == 0) { ++i; input_vector.push_back((input.at(i) + input.at(i+1))); } else { input_vector.push_back(input.at(i)); } } for(int = 0; < input_vector.size(); ++i) { cout << endl << input_vector[i]; } }
i know problem coming trying add together char vector of strings, how each char in string , maintain string store vector. or there improve way parse out??
editokay having problem problems come 12 splitting 2 separate chars "1 * 2" how go represents 12 , doesn't split up???
here solution (using c++11): #include <algorithm> #include <string> #include <vector> #include <iostream> int main() { std::string const input = "(3+(4+12))"; std::vector<std::string> chars(input.length()); // maps each character of `input` `std::string` // character , saves results in // corresponding position in `chars` vector: std::transform(input.cbegin(), input.cend(), chars.begin(), [](char c) { // 1 of ways cast `char` `std::string`: homecoming std::string(1, c); }); // sure works properly, prints // generated strings: (size_t = 0; < chars.size(); ++i) { std::cout << chars[i]; } std::cout << std::endl; }
c++ string parsing vector char
Comments
Post a Comment