判断一个数或者一串字符串是不是回文数
程序员文章站
2022-05-29 15:48:59
...
一、判断一个整数是不是字符串 例123321是回文数
#include<stdio.h>
int my_reserve(int num)
{
int sum = 0;
while (num)
{
int tmp = 0;
tmp= num % 10;
num = num / 10;
sum = tmp+sum*10;
}
return sum;
}
int main()
{
int num=0;
scanf("%d", &num);
int ret = my_reserve(num);
printf("%d", ret);
if (num == ret)
{
printf("是回文数");
}
else
{
printf("不是回文数");
}
}
判断一串字符串是不是回文数 例“123321"是回文数
#include<stdio.h>
int contrast(char *p,int len )
{
int sign = 0;
char *start = p;
char *end = (p + len - 1);
while (start <= end)
{
if (*start == *end)
{
sign = 1;
}
else
{
sign = 0;
break;
}
start++;
end--;
}
return sign;
}
int main()
{
char arr[] = "123546";
int len = strlen(arr);
int ret = contrast(arr, len);
if (ret == 1)
{
printf("是回文数");
}
else
{
printf("不是回文数");
}
}
转载于:https://blog.51cto.com/wpfbcr/1708211