# 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. ## 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. 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. 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. i think it is required to use the division and modulus command...

5. 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. 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.

7. what does this line do?
Code:
`s = z == y ? " " : " NOT ";`
and what is
Code:
`*s`
thank you for the explanation

8. 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.

9. 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.

10. 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. 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. 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.

13. Originally Posted by treenef
I think this explains that...

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

14. 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.

Popular pages Recent additions