There is a code that reveals whether a phrase or a word is a palindrome; there is such a code

#include "stdafx.h" #include "locale.h" #include <iostream> #include "windows.h" using namespace std; char slovo[80], prov[80] = { 0 }; int t = 0, i = 0, a, z=0, o=0; char* netprobelam(char *slovo) { do { if (slovo[z] == ' ') { o = z; z--; do { slovo[o] = slovo[o + 1]; } while (slovo[o] != '\0'); } z++; } while (slovo[z] != '\0'); return slovo; } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); setlocale(LC_ALL, "Russian"); cin >> slovo; do { t++; } while (slovo[t] != '\0'); a = t; t = a - 1; netprobelam(slovo); system("pause"); do { prov[i] = slovo[t]; i++; t--; } while (t != -1); t = 0; i = i - 1; do { t++; i--; } while (slovo[t] == prov[i]); if ((t - 1) == a) { cout << "Выполняется\n"; } else { cout << "Не выполняется\n"; } system("pause"); } 

the problem is that the function does not replace all the letters with the following, but only chops off the word before the first space, I ask for help.

  • Global variables are terrible. Especially when they are used for counters. - arrowd

1 answer 1

 cin >> slovo; 

It works this way - before the first space. Try getline

  getline(cin, slovo); 

just slovo make string , not an array.

So or

 cin.getline(slovo,80); 

if it needs an array.

PS But, frankly, the code should be rewritten, as they say, from the word "absolutely" ...

For example:

 bool isPalindrome(const char * s) { for(const char * b = s, *e = b + strlen(s) - 1; b <= e; ++b, --e) { while(*b == ' ') ++b; while(*e == ' ') --e; if (*b != *e) return false; } return true; } int main(int argc, const char * argv[]) { string s; getline(cin,s); cout << "\"" << s << "\" is " << (isPalindrome(s.c_str()) ? "" : "not ") << "palindrome\n"; }