两种解法:非递归和递归
#include <iostream>
#include <string>
using namespace std;
bool is_palindrome(string const& str)
{
int i = 0;
int j = str.size() - 1;
while (i < j)
{
if (str[i] != str[j])
{
return false;
}
++ i;
-- j;
}
return true;
}
bool is_palindrome_recursive(string const& str, int start, int end)
{
if (start >= end)
{
return true;
}
if (str[start] != str[end])
{
return false;
}
return is_palindrome_recursive(str, start + 1, end - 1);
}
bool is_palindrome2(string const& str)
{
return is_palindrome_recursive(str, 0, str.size() - 1);
}
void print_judge(string const& str)
{
cout << "\"" << str << "\"" << " is palindrome ?" << endl;
cout << "judge 1 : " <<(is_palindrome(str) ? "TRUE" : "FALSE") << endl;
cout << "judge 2 : " << (is_palindrome2(str) ? "TRUE" : "FALSE") << endl;
cout << "=============================================" << endl;
}
int main()
{
print_judge("abc");
print_judge("abcdefg");
print_judge("");
print_judge("a");
print_judge("abcdcba");
}
#include <iostream>
#include <string>
using namespace std;
bool is_palindrome(string const& str)
{
}
bool is_palindrome_recursive(string const& str, int start, int end)
{
}
bool is_palindrome2(string const& str)
{
}
void print_judge(string const& str)
{
}
int main()
{
}