DimovIvan

C++Advanced-Maps and Sets-Short Words

Jun 7th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. #include<sstream>
  5. #include<algorithm>
  6. #include<cctype>
  7. #include<set>
  8.  
  9. std::vector<std::string> readInput() {
  10.     std::vector< std::string > words;
  11.     std::string line;
  12.     getline(std::cin, line);
  13.     std::istringstream istr(line);
  14.     std::string currWord;
  15.     while (istr >> currWord)
  16.     {
  17.         words.push_back(currWord);
  18.     }
  19.  
  20.     return words;
  21. }
  22.  
  23. char toLower(char inputChar) {
  24.     return tolower(inputChar);
  25. }
  26.  
  27. void transformToLowercase(std::vector< std::string >& words) {
  28.     for (auto& word : words)
  29.     {
  30.         std::transform(word.begin(), word.end(), word.begin(), toLower);
  31.     }
  32. }
  33.  
  34. std::set<std::string> produceAnswerWords(const std::vector< std::string >& words) {
  35.     std::set<std::string> answerWords;
  36.  
  37.     for (auto& word : words)
  38.     {
  39.         if (word.size() < 5)
  40.         {
  41.             answerWords.insert(word);
  42.         }
  43.     }
  44.  
  45.     return answerWords;
  46. }
  47.  
  48. void printSolution(const std::set<std::string>& answerWords) {
  49.     std::ostringstream ostr;
  50.     for (const auto& word : answerWords)
  51.     {
  52.         ostr << word << ", ";
  53.     }
  54.     std::string answerStr = ostr.str();
  55.     if (!answerWords.empty())
  56.     {
  57.         answerStr.pop_back();
  58.         answerStr.pop_back();
  59.     }
  60.     std::cout << answerStr << std::endl;
  61. }
  62.  
  63. int main() {
  64.     auto wordsInput = readInput();
  65.     transformToLowercase(wordsInput);
  66.     const auto answerWords = produceAnswerWords(wordsInput);
  67.     printSolution(answerWords);
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment