1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#include <stdio.h>
#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);
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;
}
}
}
|