程序1,用函数实现:
# include <stdio.h>
# include <string.h>
int fun(char *p, char *q)
{
char *s, *t;
int n;
n = strlen(p); //计算所输入字符长度;
s = p+n-1; //将字符的位置移到串尾;
t = q; //保留逆序后的字符串的首地址;
while(n--)
*q++ = *s--; //将原字符串从串尾字符开始逆序赋给s2数组;
*q = '\0'; //给逆序后字符串添加结束标志;
printf("原字符串为:%s\n", p);
printf("逆序输出为:%s\n", t);
return strcmp(p, t); //对比两个字符串,返回其值;
}
void main(void)
{
char s1[20], s2[20];
printf("Please input string:");
gets(s1);
if(fun(s1, s2) == 0)
printf("该字符串回文!!\n");
else
printf("该字符串不回文!\n");
}
/*该程序在VC++6.0中的执行结果:
---------