Advertisement
kutuzzzov

Тема 11 урок 3

Aug 17th, 2022
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // Определяет, будет ли слово палиндромом
  7. // text может быть строкой, содержащей строчные символы английского алфавита и пробелы
  8. // Пустые строки и строки, состоящие только из пробелов, — это не палиндромы
  9. bool IsPalindrome(const string& text) {
  10.     // Напишите недостающий код
  11.     string t;
  12.     if (text.empty()) {
  13.         return false;
  14.     }
  15.     for (int i = 0; i < text.size(); ++i) {
  16.         if (text[i] != ' ') {
  17.             t += text[i];
  18.         }  
  19.     }
  20.     if (t.empty()) {
  21.         return false;
  22.     }
  23.     bool r = true;
  24.     for (int i = 0; i < t.size() / 2; ++i) {
  25.         if (t[i] == t[t.size() - 1 - i]){
  26.             r = true;
  27.         } else {
  28.             r = false;
  29.             break;
  30.         }
  31.     }
  32.     return r;
  33. }
  34.  
  35. int main() {
  36.     string text;
  37.     getline(cin, text);
  38.  
  39.     if (IsPalindrome(text)) {
  40.         cout << "palindrome"s << endl;
  41.     } else {
  42.         cout << "not a palindrome"s << endl;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement