From 54fefe795d6a0b8719c8379bd7bfaaa4c594fbd1 Mon Sep 17 00:00:00 2001 From: Brett Weiland Date: Thu, 12 Nov 2020 08:01:34 -0600 Subject: modified: a modified: makefile modified: string_shit.cpp --- a | Bin 24840 -> 195016 bytes makefile | 2 +- string_shit.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/a b/a index 74edae7..80f8744 100755 Binary files a/a and b/a differ diff --git a/makefile b/makefile index 66f7ebb..a57c35e 100644 --- a/makefile +++ b/makefile @@ -1,2 +1,2 @@ make: - g++ -o a string_shit.cpp + g++ -o a string_shit.cpp -ggdb diff --git a/string_shit.cpp b/string_shit.cpp index 4df2503..40152c9 100644 --- a/string_shit.cpp +++ b/string_shit.cpp @@ -2,11 +2,18 @@ #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); @@ -21,6 +28,25 @@ int main() { 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'){ -- cgit v1.2.3