简体   繁体   中英

Extract characters from special input

In an exercise the input is given in the following format:

{a, b, a, d, e} // some random example

The input starts with the character { and ends with } .

I want to refer to each small English letter in the standard way " x = above_input, x[0] = a, x[1] = b,...,x[4] = e ".

Is there an efficient way to this?

For example i tried to extract the small English letters and store them in a vector or i tried to convert the input somehow into a set, but i did not work out.

Use a good old for loop to recognise the characters you want and put them in a string instead of vector:

#include <iostream>
#include <string>

int main() {
    std::string ip;
    std::getline(std::cin, ip);
    std::string str;
    for (const char& character : ip) {
        if (character >= 'a' && character <= 'z') {
            str.push_back(character);
        }
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM