一、实践训练题目
1、输入一个长度小于100的字符串,判断其是否为回文串。
先上代码:
`#include<iostream>
#include<string>
using namespace std;
string abc;
int len;
bool isPalindrome = true;
//定义一个布尔变量,用来记录字符串是否是回文串
bool isPalind(string abc, int len,int m) {
if (abc[len - 1] != abc[m])
return false;
if (len > m) {
if (len == m || len - 1 == m)
return true;
return isPalind(abc, len - 1, m + 1);
}
}
//构造一个递归函数,实现判断是否为回文串,也可以用for循环实现
int main() {
while (cin >> abc) {
isPalindrome = isPalind(abc, abc.length(), 0);
//string对象.length()函数用来返回string对象的长度
if (isPalindrome)
cout << "该字符串是回文串。" << endl;
else
cout << "该字符串不是回文串。" << endl;
}
system("pause");
return 0;
}
//此题比较简单,思路清晰,只需要比较字符串中第i和第n-i位的字符是否一致就行。
`