急!!!输入一个字符串,判断该字符串是否为回文字符串。要求必须用递归

急!!!请速度帮我忙,非常感谢!!!现在没有分了,到时候想办法报答

第1个回答  2009-04-21
“递归”这个我也不是太懂。
我在网上看到过回文,使用了好多复杂的函数,我都还没学过。

不过我自己写了一个,你看看,原理很简单。
用了.substr函数 for循环 getlin(提取一句话,遇回车终止)
是在C++里写的,不知道在C里能不能用。
<这里在C++的getline(cin,stringname函数有个bug(第一次从键盘getlin输入两个回车才能getlin,但是第二个回车会保存到下一次的getlin)所以循环getline时会出错> 我把循环去掉了。

//*************************************************************
//Palindrome Judgment program 回文判断程序 U6ex4 page235
//cin a sentence,cout its characters in reverse order
//and judgment whether the input line is a palindrome
//*************************************************************
#include<iostream>
#include<string>
using namespace std;

string sen;
string pal;//palindrome回文
int len,i,j;//len=length字符串的长度,i是for循环变量,j判断倒序与正序相同字符的个数
//如果它等于字符串总的长度,即为回文。
void main()
{
cout<<"please input a sentence\n";
getline(cin,sen);

cout<<"the reverse order is:\n";
len=sen.length();
j=0; //倒序与正序相同字符的个数,赋初值0

for(i=0;i<len;i++)
{
pal=sen.substr(len-i-1,1);
cout<<pal;

if(pal==sen.substr(i,1))
j++;
}

if(j==len)
cout<<endl<<"this sentence IS a palindrome\n";
else
cout<<endl<<"this sentence is NOT a palindrome\n";

}
第2个回答  2009-04-07
你想要哪种语言啊?
我是学C++的
你可以去以“回文 C++”百度一下本回答被提问者采纳
相似回答