Advertisement
chevengur

Вводный курс: основы C++ | Урок 4: Зачем нужны функции 2/2

Aug 31st, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. // определяет, будет ли строка s палиндромом
  8. bool IsPalindrome(string s) {
  9.     for (int i = 0; i < s.size() / 2; ++i) {
  10.         if (s[i] != s[s.size() - i - 1]) {
  11.             return false;
  12.         }
  13.     }
  14.     return true;
  15. }
  16.  
  17. vector<string> PalindromeFilter(vector<string> words, int min_length) {
  18.     vector<string>search_words;
  19.     for (const auto&word: words) {
  20.         if (IsPalindrome(word) && word.size() >= min_length) {
  21.             search_words.push_back(word);
  22.         }
  23.     }
  24.     return search_words;
  25. }
  26.  
  27. int main() {
  28.     vector<string>words{"rotor", "anna", "kayak"};
  29.     auto x = PalindromeFilter(words, 5);
  30.  
  31.     for (const auto& l : x) {
  32.         std::cout << l << endl;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement