Advertisement
force1987

palindrome

Nov 14th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 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. bool res = false;
  12. if (text.empty())
  13. return res;
  14.  
  15. for (auto it1=text.begin(), it2=text.end()-1; it1 < it2;it1++,it2--) {
  16. while (*it1 == ' '&&it1<text.end()-1)
  17. it1++;
  18. while (*it2 == ' '&&it2>text.begin())
  19. it2--;
  20. if (it1 == text.end()-1 && it2 == text.begin()&&(*it1==' '&&*it2==' ')) {
  21. break;
  22. }
  23. if (tolower(*it1) == tolower(*it2)) {
  24. res = true;
  25. continue;
  26. }
  27. if (tolower(*it1) != tolower(*it2)) {
  28. res = false;
  29. break;
  30. }
  31. }
  32. return res;
  33. }
  34.  
  35. int main() {
  36. string text;
  37. getline(cin, text);
  38.  
  39. if (IsPalindrome(text)) {
  40. cout << "palindrome"s << endl;
  41. }
  42. else {
  43. cout << "not a palindrome"s << endl;
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement