检查回文输入是字符串还是整数
Check palindrome input if string or int
大家好,我想检查输入(char 或 int)polindrome 与否,但我做不到。你能帮助我吗 ?我有错误消息
"invalid conversation char to char*"
我认为这是一个简单的问题,但我无法解决它。感谢您的帮助。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
int r,sum=0,temp;
char a;
char *b = &a;
printf("Enter a string or number\n");
scanf("%c", &a);
if ( isalpha( a ) )
{
b = a;
strrev(b);
if (strcmp(a, b) == 0)
printf("The string is a palindrome.\n");
else
printf("The string isn't a palindrome.\n");
}
else if ( isdigit( a ) )
{
temp=a;
while(a>0)
{
r=a%10;
sum=(sum*10)+r;
a=a/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
}
return 0;
}
既然你提到你是一名学生,想学习C,我不会写代码,但会尽力为你指明正确的方向。
要解决这个问题,您需要:
- 获取字母数字字符串
- 检查是否是回文(你有几个选项)
首先,要获得一个字符串,您的代码中有:
char a;
scanf("%c", &a);
提示:这只会让你获得一个角色。要获取字符串,您首先需要分配一个数组而不是一个字符,然后使用带有不同参数的 scanf,而不是 %c。
这部分任务完全独立于第二部分。我建议首先确保这部分工作正常,然后再继续。您可以通过获取一个字符串然后立即打印它来实现。这样你就可以看到你实际处理的是什么。
获得字符串后,继续分析它。您可以还原它,然后与原始代码进行比较(这就是您的代码所建议的),但这样做可能更容易:
- 查找字符串长度
- 将字符串中的每个符号与其对称的对应符号进行比较。例如,如果字符串长度为 10,则需要比较符号 #0 和符号 #9,符号 #1 和符号 #8 等。提示:您需要在此处使用循环。
大家好,我想检查输入(char 或 int)polindrome 与否,但我做不到。你能帮助我吗 ?我有错误消息
"invalid conversation char to char*"
我认为这是一个简单的问题,但我无法解决它。感谢您的帮助。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
int r,sum=0,temp;
char a;
char *b = &a;
printf("Enter a string or number\n");
scanf("%c", &a);
if ( isalpha( a ) )
{
b = a;
strrev(b);
if (strcmp(a, b) == 0)
printf("The string is a palindrome.\n");
else
printf("The string isn't a palindrome.\n");
}
else if ( isdigit( a ) )
{
temp=a;
while(a>0)
{
r=a%10;
sum=(sum*10)+r;
a=a/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
}
return 0;
}
既然你提到你是一名学生,想学习C,我不会写代码,但会尽力为你指明正确的方向。
要解决这个问题,您需要:
- 获取字母数字字符串
- 检查是否是回文(你有几个选项)
首先,要获得一个字符串,您的代码中有:
char a;
scanf("%c", &a);
提示:这只会让你获得一个角色。要获取字符串,您首先需要分配一个数组而不是一个字符,然后使用带有不同参数的 scanf,而不是 %c。
这部分任务完全独立于第二部分。我建议首先确保这部分工作正常,然后再继续。您可以通过获取一个字符串然后立即打印它来实现。这样你就可以看到你实际处理的是什么。
获得字符串后,继续分析它。您可以还原它,然后与原始代码进行比较(这就是您的代码所建议的),但这样做可能更容易:
- 查找字符串长度
- 将字符串中的每个符号与其对称的对应符号进行比较。例如,如果字符串长度为 10,则需要比较符号 #0 和符号 #9,符号 #1 和符号 #8 等。提示:您需要在此处使用循环。