I wonder why the last printf
of my program is always the same? I made it in C and I'm a beginner. The purpose of the program is to verify that a
is palindrome or not. But when it comes time to print the result, in every run of the program the result is always that the number is not palindrome. How do I resolve this?
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *strrev(char *str)
{
char *p1, *p2;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
char* itoa(int i, char b[]){
char const digit[] = "0123456789";
char* p = b;
if(i<0){
*p++ = '-';
i *= -1;
}
int shifter = i;
do{ //Move to where representation ends
++p;
shifter = shifter/10;
}while(shifter);
*p = '#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *strrev(char *str)
{
char *p1, *p2;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
char* itoa(int i, char b[]){
char const digit[] = "0123456789";
char* p = b;
if(i<0){
*p++ = '-';
i *= -1;
}
int shifter = i;
do{ //Move to where representation ends
++p;
shifter = shifter/10;
}while(shifter);
*p = '%pre%';
do{ //Move back, inserting digits as u go
*--p = digit[i%10];
i = i/10;
}while(i);
}
int main(void){
int contaDigit = 0, valor;
int numberWantToCheck;
printf("What's the number you want to check?\n");
scanf("%d", &numberWantToCheck);
valor = numberWantToCheck;
do{
contaDigit += 1;
valor /= 10;
}while(valor != 0);
char stringOfTheNum[contaDigit];
itoa(numberWantToCheck, stringOfTheNum);
char reversedNum[contaDigit];
strcpy(reversedNum, strrev(stringOfTheNum));
if(strcmp(reversedNum , stringOfTheNum)){
printf("The number is palindrome.\n");
}else{
printf("The number is not palindrome.\n");
}
}
';
do{ //Move back, inserting digits as u go
*--p = digit[i%10];
i = i/10;
}while(i);
}
int main(void){
int contaDigit = 0, valor;
int numberWantToCheck;
printf("What's the number you want to check?\n");
scanf("%d", &numberWantToCheck);
valor = numberWantToCheck;
do{
contaDigit += 1;
valor /= 10;
}while(valor != 0);
char stringOfTheNum[contaDigit];
itoa(numberWantToCheck, stringOfTheNum);
char reversedNum[contaDigit];
strcpy(reversedNum, strrev(stringOfTheNum));
if(strcmp(reversedNum , stringOfTheNum)){
printf("The number is palindrome.\n");
}else{
printf("The number is not palindrome.\n");
}
}