#include #include #include #include #include using namespace std; int main() { string input; string input_mod; map char_count; //maps act sort of like an array, but insted of having numbers for indexes we can make our own indexes, called keys. //so we can say things like char_count['asdf'] = 1. while(true) { cout << "\nType in a string.\n"; getline(cin, input); input_mod = input; reverse(input_mod.begin(), input_mod.end()); cout << "\nString in reverse order:\t\t\t\t" << input_mod << "\n"; cout << "Number of characters in string:\t\t\t\t" << input.length() << "\n"; input_mod.erase(remove_if(input_mod.begin(), input_mod.end(), ::isspace), input_mod.end()); cout << "Number of characters in the string, excluding spaces:\t" << input_mod.length() << "\n"; for(char c : input) { //for every character in our string if(char_count.find(c) == char_count.end()) { //if the key doesnt exist in the char_count map, the following will execute: char_count.insert({c, 1}); //we create a key and give it a value of 1. } else { char_count[c]++; //otherwise, if the key exists, we add it by 1. } } for(auto const& c : char_count){ //for every key/value in the map, cout << (char)c.first << ":\t" << (int)c.second << "\n"; //we print the key (pair.first) and the value (pair.second) } char_count.clear(); cout << "Would you like to exit? (y/n)\n"; getline(cin, input); if(input.at(0) == 'y'){ return 0; } } }