题目要求:
任意输入一个字符串(长度不大于100),判断该字符串是否为“回文”(顺读和倒读都一样的字符串)
#include<iostream>
#include<string>
using namespace std;
int HuiWen(string a,int n)
{
int i=0,j=a.length()-1;
while(i<j)
{
if(a[i]!=a[j])
break;
i++;
j--;
}
if(i>j)
return 1;
else
return 0;
}
int main()
{
string a;
cout<<"输入任意一串字符";
cin>>a;
int n=a.length();
int y=HuiWen(a,n);
if(y)
cout<<"回文"<<endl;
else
cout<<"不是回文"<<endl;
return 0;
}
#include<iostream>
using namespace std;
#define max 100
int HuiWen(char a[],int n)
{
int i=0,j=n-1;
while(i<j)
{
if(a[i]!=a[j])
break;
i++;
j--;
}
if(i>j)
return 1;
else
return 0;
}
int main()
{
char a[max];
int n=0;
cout<<"输入任意一串字符";
/*
do
{
n++;
cin>>a[n];
char ch;
}while((ch=getchar())!='\0');//getchar用法
*/
gets(a);
n=strlen(a);
int y=HuiWen(a,n);
if(y)
cout<<"回文"<<endl;
else
cout<<"不是回文"<<endl;
return 0;
}