giGii

palindrome

Sep 6th, 2022
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 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 clear_text;
  12.     for (char c : text) {
  13.         if (c == ' ') {
  14.             continue;
  15.         }
  16.         clear_text += c;
  17.     }
  18.  
  19.     if (clear_text.size() == 0) {
  20.         return false;
  21.     }
  22.  
  23.     int length = clear_text.size();
  24.     for (int i = 0; i < length / 2; ++i) {
  25.         if (clear_text[i] != clear_text[length - i - 1]) {
  26.             return false;
  27.         }
  28.     }
  29.  
  30.     return true;
  31. }
  32.  
  33. int main() {
  34.     string text;
  35.     getline(cin, text);
  36.  
  37.     if (IsPalindrome(text)) {
  38.         cout << "palindrome"s << endl;
  39.     } else {
  40.         cout << "not a palindrome"s << endl;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment