⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 palindrome.cpp

📁 BigC++的源码
💻 CPP
字号:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

/**
   Tests whether a string is a palindrome. A palindrome
   is equal to its reverse, for example "rotor" or "racecar".
   @param s a string
   @return true if s is a palindrome
*/
bool is_palindrome(string s)
{
   // separate case for shortest strings
   if (s.length() <= 1) return true;

   // get first and last character, converted to lowercase   
   char first = s[0];
   char last = s[s.length() - 1];

   if (first == last)
   {
      string shorter = s.substr(1, s.length() - 2);
      return is_palindrome(shorter);
   }
   else
      return false;
}

int main()
{
   cout << "Enter a string: ";
   string input;
   getline(cin, input);
   cout << input << " is ";
   if (!is_palindrome(input)) cout << "not ";
   cout << "a palindrome.\n";
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -