#include #include #include using namespace std; // ======================================================= bool est_palindrome(string_view s) { if (s.size() <= 1) return true; return (s.front() == s.back()) and est_palindrome(s.substr(1, s.size()-2)); /* ou aussi : return (s.size() <= 1) or ( (s.front() == s.back()) and est_palindrome(s.substr(1, s.size()-2)) ); */ } // ======================================================= string palindromise(string_view s) { string retour(s); for(auto c : s) { retour = c + retour;} // { retour += c; } // ne marche pas : il faut aller dans l'autre sens : // for (size_t i(s.size()); i --> 0;) retour += s[i]; // si vous utilisez s.size()-1: ATTENTION à la chaîne vide !! return retour; } // ===== tests =========================================== // pour éviter le « copié-collé » dans test() ci-dessous void test_elementaire(string_view s) { cout << '"' << s << "\" : " << est_palindrome(s); } // -------------------------------------------------- void test(string_view s) { // niveau 3 // ou void test(string const& s) { // niveau 2 // ou void test(string s) { // niveau 1 test_elementaire(s); cout << " --> "; test_elementaire(palindromise(s)); cout << endl; } // ======================================================= int main() { test("abcd"); // non palindrome test("rever"); // palindrome impair test("elle"); // palindrome pair test("abcdefghfedcba"); // non palindrome, mais presque // cas limites test(""); test("a"); test("ab"); test("aa"); return 0; }