Errors in Palindrome Finder

This is a discussion on Errors in Palindrome Finder within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    5

    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. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    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!
    Last edited by Syscal; 09-07-2010 at 08:29 AM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    35
    A million digits!?

  4. #4
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Quote Originally Posted by AntP View Post
    A million digits!?
    Use a list.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,231
    Quote Originally Posted by CommonTater
    Error?
    Yes, but it is a logic error with an edge case, not a compile error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:05 PM
  3. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM
  4. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 01:14 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21