diff options
author | Brett Weiland <brett_weiland@bpcspace.com> | 2020-11-12 08:01:34 -0600 |
---|---|---|
committer | Brett Weiland <brett_weiland@bpcspace.com> | 2020-11-12 08:01:34 -0600 |
commit | 54fefe795d6a0b8719c8379bd7bfaaa4c594fbd1 (patch) | |
tree | 2387a2737af1e70dc1eb61554398d9795c2f1442 | |
parent | 2d7eb043c3aa65ce1611850fcb1ede6b480588fb (diff) |
modified: makefile
modified: string_shit.cpp
-rwxr-xr-x | a | bin | 24840 -> 195016 bytes | |||
-rw-r--r-- | makefile | 2 | ||||
-rw-r--r-- | string_shit.cpp | 26 |
3 files changed, 27 insertions, 1 deletions
Binary files differ @@ -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 <iostream> #include <bits/stdc++.h> #include <string> +#include <map> + using namespace std; int main() { string input; string input_mod; + + map<char, int> 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'){ |