fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. char *strrev(char *str)
  6. {
  7. char *p1, *p2;
  8.  
  9. if (! str || ! *str)
  10. return str;
  11. for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  12. {
  13. char temp = *p1;
  14. *p1 = *p2;
  15. *p2 = temp;
  16. }
  17. return str;
  18. }
  19.  
  20. int main(void) {
  21. int numberWantToCheck;
  22. printf("What's the number you want to check?\n");
  23. scanf("%d", &numberWantToCheck);
  24. int bufferSize = snprintf(NULL, 0, "%d", numberWantToCheck) + 1;
  25.  
  26. char stringOfTheNum[bufferSize];
  27. char reversedNum[bufferSize];
  28. snprintf(stringOfTheNum, bufferSize, "%d", numberWantToCheck); // converte numero para String
  29. strcpy(reversedNum, stringOfTheNum); // copia a string para reversedNum
  30. strrev(reversedNum); // inverte reversedNum
  31.  
  32. if(strcmp(reversedNum, stringOfTheNum) == 0) {
  33. printf("The number is palindrome.\n");
  34. } else {
  35. printf("The number is not palindrome.\n");
  36. }
  37. }
Success #stdin #stdout 0s 9424KB
stdin
113211
stdout
What's the number you want to check?
The number is not palindrome.