# Thread: Errors in Palindrome Finder

1. ## Errors in Palindrome Finder

Ok, so I tried the exercise over on https://www.spoj.pl/problems/PALIN/
The rules state:
"A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros."

I gave it my best shot, fellas, but it just won't compile. have a ........ing field day, I'm spent for today.

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

void input(int *numberPointerOne);
int test(int *numberPointerTwo);
int *reverse(int*orig, unsigned short int b);

int main (int argc, const char * argv[]) {
int number[1000000];
int returned = 0;
int lengthTwo;
input(number);

while(returned = 0){
returned = test(number);

lengthTwo = strlen(number);
reverse(number, lengthTwo);
}

printf("%d", returned);
return 0;
}

void input(int *numberPointer){
printf("enter positive integer:\t");
scanf("%d", numberPointer);
fpurge(stdin);
}

int test(int *numberPointerTwo){
int lengthOne;
int before, after;
before = atoi(numberPointerTwo);
lengthOne = strlen(numberPointerTwo);
after = reverse(numberPointerTwo, lengthOne);

if(before == after){
return after;
} else{
return 0;
}
}

int *reverse(int *orig, unsigned short int b){
unsigned short int a=0;
int swap;
for(a;a<--b;a++){
swap=orig[a];
orig[a]=orig[b];
orig[b]=swap;
}
return orig;
}

2. Something like this works, though it could most likely be better:

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

typedef enum{false, true}bool;

bool is_palendrome(char* str){

char* b = str, *e = str + strlen(str) - 1;

while(*b == *e && b <= e)printf("%c == %c\n",*b++,*e--);

return ((*b == *e) ? true : false);
}

int main(){

char buffer[1024];

fgets(buffer,sizeof(buffer),stdin);

buffer[strlen(buffer) -1] = '\0';

printf("%s\n",((is_palendrome(buffer)) ? "Is a palendrome" : "Is not a palendrome"));

return 0;
}
Where you check character by character for equality. I know someone will likely find something wrong with it. But it does the basic idea and is case sensitive. Also, your array is wayy to big!

3. A million digits!?

4. Originally Posted by AntP
A million digits!?
Use a list.

5. Code:

int *reverse(int *orig, unsigned short int b){
unsigned short int a=0;
int swap;
for(a;a<--b;a++){   <--- Error?
swap=orig[a];
orig[a]=orig[b];
orig[b]=swap;
}
return orig;

6. Originally Posted by CommonTater
Error?
Yes, but it is a logic error with an edge case, not a compile error.

7. Originally Posted by laserlight
Yes, but it is a logic error with an edge case, not a compile error.
Yeah, that's why the question mark... I don't have time to play with this today but I thought maybe that's where the problem was.