Thread: Palindrome in C

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    48

    Palindrome in C

    Hello!
    Was just wondering if there's a C program which can take from the user a number and it checks if its a palindrome or not...I know that we %10
    right ?
    May you show it to me!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yeah, we could, but that kind of defeats the purpose, doesn't it? You learn best if you actually write the code out, which could probably be done within one day. If you're really stuck, you could find it on google probably.

  3. #3
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I could show you a palindrome program but I need to write a paper on the Boston tea party. I know it involves tea. And Boston. Could you write it for me?

    Thanks!

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Well..if you get into details about what you want me to write..would be my pleasure
    as for the palindrome I will do the Macgyver said..I will try first and when I give up I will come in here

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Here's an advice, DaniiChris; it's easier to check if a string is a palindrome than if a number is. So here's an idea of how your program should looks like (I'm giving you the design, you'll have to implement the algorithms)

    Code:
    int isPalindromeString(const char *s)
    {
        // This function should return 1 if the string is a palindrome, else 0
    }
    
    int isPalindromeInt(int n)
    {
        // This function should return 1 if the integer (or it's absolute value) is a palindrome, else 0
        // Note that this function should call the isPalindromeString function. It just need to transform the int into a string
    }
    And here's some functions from the C library which could help you resolve this problem:


    Now, show us what a 12-year old can do.
    I hate real numbers.

  6. #6
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    My idea was to place the input into an array (easy to do if you can use getchar to collect input). Then just iterate through the array.

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by noops View Post
    My idea was to place the input into an array (easy to do if you can use getchar to collect input). Then just iterate through the array.
    Then just iterate through the array and exit? Wow, you should get a cookie for some specific instructions.

    Using getchar to complicate things would be .. superfluous. I'd just use fgets and then reverse the string, store it elsewhere, and compare it to the original string. You can also just iterate through the array using two indexers - one starting at the start, and the other at the end, and compare the characters in both positions.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If a number has k digits, (which you should know if you've converted it to a string) then compare every digit i to k - i. If they're all equal, you have a palindrome. Not hard.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Thanks everyone! Will post my code as soon as I get it done!
    I totally apreiciate every single one in here

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    I gave it a try! and here it is
    Thanks to everyone
    Code:
    #include <stdio.h>
    int main()
    {
    int x,num,p,rev;
    printf("Enter your desired number you want to check");
    scanf("%i",&num);
    
    while(num > 0)
    	{
    	p = num % 10;
    	rev = num*10 + p;
    	rev = num / 10;
    	rev = x;
    	}
    
    if (x == rev)
    printf("Yes,Its a Palindrome");
    else
    printf("No,It isn't a Paindrome");
    return(0);
    }

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What is the value of x supposed to be? You never assign it a value.

  12. #12
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    &#37;10 will only give you the last digit. Also, you are never updating the num variable in your while loop so it will probably always be an infinite loop. Here's how you do it using math:

    Code:
    while (num >= 10)
    {
    	for (i = 10; i * 10 < num; i *= 10);
    	if (num / i != num % 10)
    		palindrome = 0;
    	num = (num - (num / i) * i) / 10;
    }
    Last edited by noops; 07-24-2008 at 05:08 PM.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    I came up with this in about 5 minutes. Of course it doesn't work, because I intentionally broke it. May you fix it?

    (Code highlighting courtesy of dwks' Codeform!)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int is_palindrome(int number) {
       int n = abs(number);
       int r = 0;
       
       while (n > 0) {
          r = r + n%10;
          n /= 10;
       }
       
       return r == number;
    }
    
    int main(int argc, char **argv) {
       int num;
       
       if (argc > 1)
          sscanf(argv[1], "%d", &num);
       else {
          printf("Enter a number: ");
          fflush(stdout);
          scanf("%d", &num);
       }
       
       printf("%s", is_palindrome(num) ? "YES" : "NO");
       putchar('\n');
       return 0;
    }

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I still like my way better:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int palindromicity ( const char * numbuff )
    {
        size_t i, len;
        for( i = 0, len = strlen( numbuff ); i < len; i++ ) {
            if( numbuff[i] != numbuff[len - 1 - i] ) return 0;
        }
    
        return 1;
    }
    
    int main ( void )
    {
        char numbuff[32];
        printf( "Please enter a integer:\n" );
        fgets( numbuff, sizeof numbuff, stdin );
        numbuff[strcspn( numbuff, "\n" )] = '\0';
    
        printf( "&#37;s is%s palindromatic.\n", numbuff,
            palindromicity( numbuff ) ? "" : " NOT" );
    
        return 0;
    }
    You could verify that the input was a number but it really doesn't have an effect on the program's outcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 12:36 PM
  2. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM
  3. bool palindrome definition
    By justinc911 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 05:50 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 02:14 PM