[英]Check palindrome input if string or int
Hi everyone I want check input(char or int) polindrome or not but I can't do this.大家好,我想检查输入(char 或 int)polindrome 与否,但我不能这样做。 Can you help me?
你能帮助我吗? I have error message
我有错误信息
"invalid conversation char to char*"
I think this is a simple problem but I couldn't solve it already.我认为这是一个简单的问题,但我已经无法解决。 Thank you for your help.
谢谢您的帮助。
#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;
}
Since you mentioned that you are a student and want to learn C, I am not going to write a code but will try to point you into the right direction.既然您提到您是一名学生并想学习 C,我不打算编写代码,但会尝试为您指明正确的方向。
To solve this, you need:要解决这个问题,您需要:
First of all, to get a string, you have in your code:首先,要获取一个字符串,您的代码中有:
char a;
scanf("%c", &a);
A hint: this is only getting you one character.提示:这只会给你一个角色。 To get a string, you first need to allocate an array instead of one single char, and then use scanf with a different argument, not %c.
要获取字符串,首先需要分配一个数组而不是一个字符,然后使用带有不同参数的 scanf,而不是 %c。
This part of the task is completely independent of the second part.这部分任务完全独立于第二部分。 I recommend first to make sure that this part works before proceeding further.
我建议首先确保这部分工作,然后再继续。 You can do it by getting a string and then immediately printing it.
您可以通过获取一个字符串然后立即打印它来做到这一点。 This way you can see what you are actually dealing with.
通过这种方式,您可以看到您实际处理的内容。
Once you have your string, proceed with analyzing it.一旦你有了你的字符串,继续分析它。 You could revert it and then compare to original (that's what your code is suggesting), but it's probably easier to do something like this:
您可以将其还原,然后与原始文件进行比较(这就是您的代码所建议的),但这样做可能更容易:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.