five digit palindrome

This is a discussion on five digit palindrome within the C Programming forums, part of the General Programming Boards category; Hi... I told my friend that i would help him with a problem... unfortuantely my programming skills are VERY limited. ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    6

    five digit palindrome

    Hi... I told my friend that i would help him with a problem... unfortuantely my programming skills are VERY limited.

    The problem requires the user to enter a five digit integer and then the program is supposed to check to see if it is a palindrome. I have searched but all of the threads are for letters and words.

    I don't know where to begin... because the hint says that i should use the division and modulus commands??

    Should i have the user input each number seperately? I have no idea what i am doing... Please help me as i want to make a good impression on this "friend"...thanks

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    i think that i figured out the division and mod commands..however i don't know how to write it as a program...


    d1 = d1 \ 10000
    d2 = (d2 \ 1000) Mod 10
    d3 = (d3 \ 100) Mod 10
    d4 = (d4 \ 10) Mod 10
    d5 = d5 Mod 10

    If d1 = d5 And d2 = d4

  3. #3
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Well, this would be most easily done by creating a reversed number and then testing for equality. If the number and its inverse are identical, then the number is clearly a palindrome:
    Code:
    void is_palindrome ( int x )
    {
      int y = 0, z = x;
      char *s;
    
      do
        y = y * 10 + x % 10;
      while ( x /= 10 );
    
      s = z == y ? " " : " NOT ";
      printf ( "%d is%sa palindrom\n", z, s );
    }
    Code written to get you in trouble if you copy it and turn it in as homework.

    Cheers!

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    i think it is required to use the division and modulus command...

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by sashax415
    i think it is required to use the division and modulus command...
    well excatly, u have to use '/' and '%' operator to find the the palindrom. thats what slacker is done. for you informaton the '/' operaotr takes a off a digit from the number and '% operaor gvies a copy of a last digit
    Code:
    int num=123;
    
    num %10 --> 3;
    num/10 --> 12;
    hope u know these things before

    ssharish2005

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a lookup table. Or a big switch.
    Code:
    switch( pal )
    {
    #define C case
        C 10001:C 10101:C 10201:C 10301:C 10401:C 10501:C 10601:C 10701:C 10801:C 10901:
        ...you fill in the rest...
        C 99099:C 99199:C 99299:C 99399:C 99499:C 99599:C 99699:C 99799:C 99899:C 99999:
    #undef C
            printf("Yup!\n");
        break;
        default:
            printf("Nope!\n");
    }


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    what does this line do?
    Code:
    s = z == y ? " " : " NOT ";
    and what is
    Code:
    *s
    thank you for the explanation

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by sashax415
    what does this line do?
    Code:
    s = z == y ? " " : " NOT ";
    Compares z for equality to y, if true, assigned " " to s, if false, assigns " NOT " to s.
    and what is
    Code:
    *s
    thank you for the explanation
    It dereferences s, look up pointers in your favourite book/reference.

    Edit: clarification, s = " "; will make the pointer s point to the string literal of " ", rather than "assigning" it.
    Last edited by cwr; 12-14-2005 at 10:20 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int ispal( int x )
    {
        char buf[BUFSIZ]={0};
        sprintf( buf, "%d", x );
        if( strlen( buf ) != 5 )
            return 0;
        return buf[0] == buf[4] && buf[1] == buf[3];
    }



    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Originally Posted by sashax415
    what does this line do?
    Code:
    s = z == y ? " " : " NOT ";

    I think this explains that...

    Code written to get you in trouble if you copy it and turn it in as homework.

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    I did something simliar.. Check this out:

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    #define TRUE 1
    #define FALSE 0
    #define SIZE 256
     
    int paliTest(char[], int high, int low); // Defines the function (accepts input
                                             // of string, high-size of string and
                                             // and low -beginning of array
    char sopal[SIZE];                        //these are temporary arrays that store
                                             // temporary char data
    char lowpal[SIZE];
    int a;
    int i;
    int main() 
    
    {
    	
        char str[SIZE]; //setting the size of str to SIZE
        printf("Please enter a palindrome\n"); // Prompts user for palidrome
        scanf("%s",&str);      // scans it into a character array called string
        
        
    	int high = 0; // sets high to 0
        int low = 0;  // sets low to 0
        int i = 0;    // sets i to 0
        	
    	high=strlen(str)-1; // sets high to the number of indexes in str -1 to account
    	                    // for /01 character at end
    	
    	
        for(i=0,a=0;i<high;i++) //for test while i is less than high (which is defined
        {                       //above
            if(isalnum(str[i]))// if str[index] is a alphanumeric character copy it
                               // to a temporary character array
            {
                 sopal[a]=str[i];
                 a++;  // increment the index of the temporary character array
            
            }
            
        }
        
    
    for(i=0,a=0;i<high;i++) //for test while i is less than high 
     {
     
      str[i] = tolower(sopal[i]); //copies back the string into character array str
                                  // while converting any characters from upper case
                                  // to lower
      
      
     }
        
        if (paliTest(str,high,low)) // If the function is true
        {
    		printf("\nYou entered a palindrome.\n"); //display palindrome detected
    		
        }
            else  //else
            {
    		printf("\nYou did not enter a palindrome.\n"); // string is not a palindrome
         	}
    	
        system("pause"); // pauses screen so user can read
    	return 0; 
    }
    
    
    int paliTest(char str[],int low,int high) // Defines the function (accepts input
                                             // of string, high-size of string and
                                             // and low -beginning of array
                                            
    {
    
           
            
        
       if(str[low]==str[high]) // if the first and last element of the array is equal
                               // return true
       {
       paliTest(str,low+1,high-1);//Recursion which then compares the next two elements
                                  // of the array on either end
       return TRUE;               // returns true which indicates that it is a palindrome
       }
       else                       // else
       {
       return FALSE;              // returns false which indicates its not a palindrome
       
    }
    }

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have a few problems with your recursive method.
    1 - You never tell it when to stop recursing, other than when str[this] != str[that].
    This means that this scenario will play out:
    Code:
    if( str[ 5 ] == str[ -1 ] )
    and potentially, other out of bounds tests.

    2 - If the first and last are the same, but the others aren't, it still will return TRUE.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    Quote Originally Posted by treenef
    I think this explains that...

    So what is the correct way to write this
    Code:
    s = z == y ? " " : " NOT ";

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no "correct" way. Or rather, there is no "one correct way" to do it. Just like displaying something to stdout. There are many functions you can use to do it. Whatever you choose is up to you, and should be based somehow on what you actually want to get done, and how hard you want it to be to get it done.

    The line shown is a perfectly fine way to do it. If you know what it does. That's the whole point of it. You figure out how to do it your way, or get a good enough understanding of everything that is happening, so that when you turn it in for homework, you can prove to your teacher that you didn't just copy someone.


    Quzah.
    Hope is the first step on the road to disappointment.

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. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 06:31 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  5. Roman number converter
    By BalanusBob in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 07:29 AM

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