如果一个字符串是回文的话,那么它一定必须是中间对称的。所以我们需要求字符串的中点。
#include "stdio.h"
#include "string.h"
main(){
char str[50], stack[50];//a数组存放字符串, stack数组是定义的栈
int i, len, mid, next, top;
gets(a); // 读入一行字符串
len = strlen(a);//字符串的长度
mid = len / 2-1;//字符串的中点
top = 0;//栈的初始化
for(i = 0; i < mid; i ++)
s[++top] = a[i];//mid前面的字符依次入栈
//需要去判断字符串长度是奇数还是偶数
if(len % 2 == 0)
next = mid + 1;
else
next = mid + 2;
for(i = next; i < = len - 1; i ++)//开始匹配
{
if a[i] != s[top]
break;
top -- ;
}
if(top == 0)
printf("The string is huiwen");
else
printf("The string is not huiwen");
while(1);
return 0;
}