0

Hi everyone I want check input(char or int) polindrome or not but I can't do this. 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;
}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 1
    C-Strings are stored in arrays. I think you want `a` to be a string: `char a[100];` And access individual characters with indexing: `a[0], a[1], etc`. – 001 Jan 05 '21 at 18:08
  • 2
    @Eyüp Şirin This assignment b = a; does not make a sense. b is a pointer while c is a character. – Vlad from Moscow Jan 05 '21 at 18:09
  • so can you write code ? i dont understand ? – Eyüp Şirin Jan 05 '21 at 18:10
  • Also, there is no need for special handling of numbers if you read it as a string. – 001 Jan 05 '21 at 18:10
  • 1
    I strongly suggest you invest in [a good book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), Whatever resource is being used to teach you C now is not really making very important topics stick. Unrelated, `strrev` isn't in the standard C library (and you shouldn't need it anyway). – WhozCraig Jan 05 '21 at 18:11
  • You read characters as input no matter if they are digits, alpha, whitespace or punctuation. There is no need to treat digits (or any other character) differently to determine if a string is a palindrome. – David C. Rankin Jan 05 '21 at 18:11
  • You cannot pass single characters to functions like `strrev()` and `strcmp()` which need null-terminated strings. – Weather Vane Jan 05 '21 at 18:14
  • If the `strrev` function does an in-place reversal, your logic will fail. You need a copy and reverse that to compare with original. – 001 Jan 05 '21 at 18:16
  • @WhozCraig i am a 1.class student.This is my homework and i'm trying to learn, dear know very well – Eyüp Şirin Jan 05 '21 at 18:18
  • You scan only one character, there's nothing to reverse. If you scan a string into a char array, then you don't need to distinguish between ints and chars, you can reverse both with the same code. – anotherOne Jan 05 '21 at 19:41

1 Answers1

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.

To solve this, you need:

  1. Get a alphanumeric string
  2. Check if it's a palindrome (you have several options there)

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.

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:

  1. Find string length
  2. Compare each symbol in the string with its symmetrical counterpart. For example, if string length is 10, you'll need to compare symbol #0 with symbol #9, symbol #1 with symbol #8 etc. etc. Hint: you'll need to use a loop here.
ZenJ
  • 313
  • 3
  • 15