Home > Back-end >  Unexpected blank output in c
Unexpected blank output in c

Time:09-01

I'm a pseudo programmer that is having such a bad time with empty terminal outputs in c language... this isn't the first time I got this issue, so I kindly as for some experienced mind to gimme a hand and analyze this code, to see whether if I'm dumb, or if I got a compiler problem or idk what...

well the code is:

#include<stdio.h>
#include<stdlib.h>

char PalindromeCheck (int number){
   int NumCopy = number;
   while (NumCopy != 0){
      int LastDigit = NumCopy % 10;
      NumCopy = NumCopy / 10;
      if (LastDigit != NumCopy % 10){
         return "Not a Palindrome";
      }
   }
   return "Palindrome";
}
int main(void)
{
   printf("Enter a number: ");
   int n = scanf("%d", &n);
   printf("%s", PalindromeCheck(n));
   return 0;
}

Thank y'all and cheers from Brazil

(Feedback about readability and good practices would also be appreciated!!!!)

CodePudding user response:

first of all , function called PalindromeCheck , should return char* not char , return just char means that you want to return just one character like you are returning c or b or a , it's just like only one character not a full string , but returning a char* means that you are returning a pointer that points to a character or points to the first character in the string , that's how you can return string , other problem is in line int n = scanf("%d", &n); and then you pass n as argument to the function , please refer to enter image description here enter image description here

CodePudding user response:

Ignoring the sadly broken algorithm, this should be it! hope this helps!!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char* PalindromeCheck (int number){
   char* toReturn = (char*) malloc(sizeof(char) * 17);
   int NumCopy = number;
   while (NumCopy != 0){
      int LastDigit = NumCopy % 10;
      NumCopy = NumCopy / 10;
      if (LastDigit != NumCopy % 10){
         strncpy(toReturn,"Not a Palindrome", 17);
         return toReturn;
      }
   }
   strncpy(toReturn,"Palindrome", 11);
   return toReturn;
}
int main(void)
{
   printf("Enter a number: ");
   int n;
   scanf("%d", &n);
   printf("%s\n", PalindromeCheck(n));
   return 0;
}
  • Related