本程序中使用指针来比较字符串的左边和右边第几位是否相同,如果都相同,则回文。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
bool check(char *str);
int main()
{
printf("请输入一个字符串:\n");
char str[80];
gets(str);
printf("\n");
puts(str);
if(check(str))
printf("该字符串回文\n");
else
printf("该字符串不回文\n");
system("pause");
return 0;
}
bool check(char *str)
{
int i=0,len=strlen(str);
char *tempt=str; //将被检测字符串的首地址赋值给tempt
for(i=0;i<len/2;i++)
{
if(*(tempt+i)!=*(tempt+len-1-i))
return false;
}
return true;
}