Advertisement
Papasalt

A program that analyses text and prints how many of each character there is

Oct 1st, 2020 (edited)
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <algorithm>
  6. #include <cctype>
  7. #include <iterator>
  8.  
  9.  
  10. template <typename Map_Keys_T, typename Map_Values_T>
  11. void printMap(std::map<Map_Keys_T, Map_Values_T> input_map) { // Iterates through map, printing key and value with a indent between, takes new line for each element
  12.     for (auto &x : input_map) {
  13.         std::cout << x.first << "\t" << x.second << "\n";
  14.     }
  15. }
  16.  
  17.  
  18. int countChars(std::string input, char x) { // Counts the amount of times the input character appears in text, and returns that value
  19.     int value = 0;
  20.     for (char letter : input) {
  21.         if (x == letter) {
  22.             value++;
  23.         }
  24.     }
  25.     return value;
  26. }
  27.  
  28. template <typename Vector_T>
  29. bool isInVector(std::vector<Vector_T> char_vector, Vector_T element_to_search) {
  30.     for (Vector_T element : char_vector) {
  31.         if (element == element_to_search) {
  32.             return true;
  33.         }
  34.     }
  35. }
  36.  
  37. std::string to_lowercase(std::string text) { // Converts input string to lowercase, and returns the converted text
  38.     std::transform(text.begin(), text.end(), text.begin(),
  39.         [](unsigned char c) { return std::tolower(c); });
  40.     return text;
  41. }
  42.  
  43. template <typename Key_T, typename Value_T>
  44. std::map<Key_T, Value_T> mapZipper(std::vector<Key_T> keys, std::vector<Value_T> values) { // Zips two vectors together into a map variable and returns the map
  45.     std::map<Key_T, Value_T> final_map;
  46.     for (int i = 0; i < keys.size(); i++) {
  47.         final_map.insert({keys[i], values[i]});
  48.     }
  49.     return final_map;
  50. }
  51.  
  52. std::map<char, int> sentenceAnalyzer(std::string sentence) { // Analyzes the text, returns a map with the structure: {letter : number_of_times_letter_appears_in_text}
  53.     std::map<char, int> amount_of_letters_map;
  54.     sentence = to_lowercase(sentence);
  55.     std::vector<char> characters;
  56.     std::vector<int> characters_amount;
  57.  
  58.     for (auto letter : sentence) {
  59.         int number_of_letters_in_text = countChars(sentence, letter);
  60.         if (isInVector(characters, letter) != true) {
  61.             characters.push_back(letter);
  62.             characters_amount.push_back(number_of_letters_in_text);
  63.         }
  64.     }
  65.     amount_of_letters_map = mapZipper<char, int>(characters, characters_amount);
  66.     return amount_of_letters_map;
  67. }
  68.  
  69.  
  70. int main() {
  71.     std::string sentence;
  72.     std::map<char, int> results;
  73.     std::map<char, int>::iterator it;
  74.     std::cout << "Enter a sentence: ";
  75.     std::getline(std::cin, sentence);
  76.  
  77.     results = sentenceAnalyzer(sentence);
  78.  
  79.     printMap<char, int>(results);
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement