summaryrefslogtreecommitdiff
path: root/string_shit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'string_shit.cpp')
-rw-r--r--string_shit.cpp26
1 files changed, 26 insertions, 0 deletions
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'){