#include #include #include using namespace std; // ======================================================= bool est_palindrome(string_view s) // ou bool est_palindrome(string const& s) { // niveau 2 // ou bool est_palindrome(string s) { // niveau 1 { if (s.size() <= 1) return true; if (s.back() != s.front()) return false; // ou : if (s[0] != s[s.size() - 1]) return false; return est_palindrome(s.substr(1,s.size()-2)); } // ======================================================= string palindromise(string_view s) { string retour(s); for(auto c : s) { retour = c + retour; } /* ou aussi (mais préférez le « for(auto :) » ) : for (size_t i(0); i < s.size(); ++i) retour = s[i] + retour; */ 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; }