//思路:先把字符串完全倒过来 用字符串比对函数,再比较前后两字符串是否相等;
#include<stdio.h>
#include<string.h>//strlen
#include<stdlib.h>//malloc
void stringtest(char *src)
{
// char *src="hello.world";
char *dest=NULL;
int len=strlen(src);//len = 11
dest=(char *)malloc(len+1);//
printf("%d\n",len );
char *d=dest;
char *s=&src[len-1];//指针下标法 src[0];src[1];src[2]...src[len-1]
while(len-- != 0)
{
*d++=*s--;// *d = *s; d--;s--
}
*d = '\0';
printf( "%s\n",dest);
int length =strlen(dest);
printf("%d\n",length/2 );
int num = strncmp(dest,src,length/2);
free(dest);//malloc();free()
if( num == 0)
{
printf("this string is a palindrome\n");
}else
{
printf("this string is not a palindrome\n");
}
}
int main() {
char p[128];
gets(p);
stringtest(p);
return 0;
}
结果:/eg1
aba
3
aba
3
this string is a palindrome
eg2
abcdefg
7
gfedcba
7
this string is not a palindrome
思路来源