There are several ways to determine if a string is a palindrome or not. Typically, you must first ignore all spacing, capitalisation and punctutation within the string, which can be done by copying the string, character by character, ignoring all non-alphanumeric characters. You then point to the first and last characters in the modified string and, so long as both characters under the pointers are the same, work the pointers towards the middle of the string until the pointers either meet in the middle or they pass each other. That is, if the string has an odd count of characters, the pointers will meet at the middle character, otherwise they will pass each other. At that point you can say with certainty the string is a palindrome. If the characters under the pointers differ at any time, then the string is not a palindrome. This is fairly straightforward to program.
A more interesting problem is when you have to locate the longest palindrome within a string which is not itself a palindrome. For instance, the string "Madam, I'm Adam is a palindrome" is not a palindrome, but it does contain one: "Madam I'm Adam". In this case we cannot point to the first and last characters and work towards the middle. Instead, we have to test every possible substring of the string. We do this by starting at the first character and treat it as if it were actually the middle character of a palindrome, and then move our pointers to the left and right of this character while the characters match. When they no longer match, or one of the pointers has reached either end of the string, we store the longest palindrome found up to that point and then move onto the next character and treat it as the middle character. If we continue in this manner, treating every character as if it were the middle character of a palindrome, we will eventually locate the longest palindrome.
The problem with this approach is when the longest palindrome has an even number of characters instead of an odd number. To get around this we simply place a single space between each character, and treat each of those as being the middle character as well. When a palindrome is found, we simply remove the spaces. In this way we can use exactly the same algorithm to cater for both odd and even character palindromes.
The only remaining problem is when we wish to print the palindrome itself. Since this will be a substring of the original string, we cannot use the modified string we used to locate the palindrome. One way to get around that is to store the original positions of each letter in an array of indices, and use that array to determine where the substring lies with in the original string.
The following program demonstrates this technique in full. The key function is the ispalindrome() function, which accepts a lower-case copy of the string (including the original spacing an punctuation), and a vector that contains the indices of each letter within the string (ignoring puctuation and spacing), separated by -1 values (representing the implied spaces between each letter). The pos value tells the function which index of the vector is to be treated as the middle character of the potential palindrome, while x and y are output parameters that determine the start and end of the palindrome within the vector. The function returns true if a palindrome was found, and the x and y values can be used to extract the palindrome from the original string, using the indices stored in the vector. Note that when the search for a palindrome fails, we step back the x and y indices by one, and if the vector index is -1, then we step back another index. We then test the x and y values to see if they indicate a palindrome was found or not.
The strip() function is another key function. This generates the vector from the lower case copy of the original string. Although we could eliminate the -1 values at the start and end of the vector, it's simpler to just leave them in.
You will note that the program can cater for strings that are themselves palindromes, as well as strings that contain palindromes.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string input_string(string prompt)
{
cout<<prompt<<":\t";
string input;
getline(cin, input, '\n');
return(input);
}
void convert_tolower(string& s)
{
for(string::iterator i=s.begin(); i!=s.end(); ++i)
*i=tolower(*i);
}
vector<int> strip(const string& s)
{
vector<int> v;
v.push_back(-1);
for(int i=0; i<s.size(); ++i)
{
if((s[i]>='a' && s[i]<='z') (s[i]>='0' && s[i]<='9'))
{
v.push_back(i);
v.push_back(-1);
}
}
return(v);
}
bool ispalindrome(const string s, const vector<int> v, int pos, int& x, int& y)
{
for(x=pos,y=pos; x>=0 && y<v.size(); --x, ++y)
if( v[x]!=-1 && ( s[v[x]]!=s[v[y]] ))
break;
++x, --y;
if( v[x]==-1 )
++x, --y;
return(x>=0 && x<y && y-x>1);
}
int main()
{
string input;
while(1)
{
input=input_string("Enter a string");
if(input.size()==0)
break;
string copy(input);
convert_tolower(copy);
vector<int> v=strip(copy);
string pal;
int pos=0;
for(int i=0; i<v.size(); ++i)
{
int start=0, end=0;
if( ispalindrome( copy, v, i, start, end))
{
string tmp( input.substr(v[start],v[end]-v[start]+1));
if( tmp.size() > pal.size() )
{
pal = tmp;
pos = v[start];
}
}
}
if( pal.size() )
{
cout<<"Palindrome:\t";
for(int i=0; i<pos; ++i)
cout<<" ";
cout<<pal<<"\n"<<endl;
}
else
cout<<"The string contains no palindromes!\n"<<endl;
}
return(0);
}
Example output:
Enter a string: Madam, I'm Adam
Palindrome: Madam, I'm Adam
Enter a string: Madam, I'm Adam is a palindrome
Palindrome: Madam, I'm Adam
Enter a string: In girum imus nocte et consumimur igni
Palindrome: In girum imus nocte et consumimur igni
Enter a string: 0123456765432
Palindrome: 23456765432
Enter a string:
Press any key to continue . . .
Write a function which gets the length of the number, then runs through the numbers at either end. It should look something like this:
string convertInt(int number)
{
stringstream ss;
ss << number;
return ss.str();
}
bool palindromeFunction(string str)
{
bool r;
int l = str.size() / 2;
int front = 1;
int back = str.size();
for (front = 1; front < l; front++)
{
back = str.size() - (front - 1);
string a = str.at(front - 1);
string b = str.at(back - 1);
if (a != b)
{
return false;
}
}
return true;
}
int main()
{
int testNumber;
string str = convertInt(testNumber);
bool palindrome = palindromeFunction(str);
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
int reverse_check(int n);
int n;
printf("enter the number");
scanf("%d",&n);
if(reverse_check(n)==n)
printf("the entered number is a palindrome\n");
else
printf("the entered number is not a palindrome\n");
getch();
}
int reverse_check(int n)
{
int r,s;
s=0;
while(n>0)
{
r=n%10;
n=n/10;
s=s*10+r;
}
printf("reverse='%d'",s);
return(s);
}
#include<iostream.h>
#include<conio.h>
void main()
{int n,num,d,r=0;
cout<<"Enter the number";
cin>>n;
do{d=n%10;
r=r*10+d;
n=n/10;
}while(n!=0)
if(num==r)
cout<<"No is palentrome";
else
cout<<"no is not palentrome";
}
A number is a palindrome if it is the same from the front and the back. Eg: 121, 12321, etc are palindromes.
You can use the following method to check if a number is a palindrome:
1. Create a copy of the original number(num) in a temporary variable(temp). Also create a number palin initialized with 0
2. Using while loop, recursive extract the digits from temp(temp%10) and set it as the least significant digit in palin( pallin = palin*10 + temp%10)
3. Discard the last digit in temp( temp = temp / 10)
4. Your while loop should terminate when temp num. If yes, then the number is a palindrome, else it is not.
To check if a number is a palindrome using pointers you first need to convert the number to a character array (a string), with one digit per character. You can then test the string just as you would any other string:
bool is_palindrome (char* str) {
char *end;
end = str + strlen (str) - 1;
while ((str < end) && (*str == *end)) ++str, --end;
return *str == *end;
}
There is no limit to the number of functions you can have in a program. The only practical limit is dependant upon the amount of memory you have available in order to load the compiled program, whether it has 4 functions or 4 trillion functions. If the program makes use of dynamic libraries, then the amount of available memory reduces accordingly.
Palindrome number is a number like 121 which remains the same when its digits are reversed. To find this number in a simple java program, just follow the below way. sum = 0; while(n>0) { r=n % 10; sum=concat(r); n=n / 10; } print r;
class test { public static void main(int num) { int num2=num; int rnum=0; while (num2>0) { int q=num2/10; int dig=num2%10; rnum = rnum*10+dig; num2=q; } if (rnum==num) System.out.println("Palindrome number"); else System.out.println("Not a Palindrome number"); } }
This program only suits PHP. If you want a proper one try C program for it available on web <body> <?php if(isset($_POST['submit'])) { $text = $_POST['text']; $string = mysql_real_escape_string($text); $invert = strrev($string); if($string == $invert) { echo "<br>Given number is a palindrome!!"; } else { echo "<br>Given number is not a palindrome!!"; } } ?> <form method="post"> <input type="text" name="text" id="text" /> <input type="submit" name="submit" value="Submit" id="submit" onclick="<?php $_SERVER['PHP_SELF']; ?>" /> </form> </body>
#include <stdio.h> #include <string.h> #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; } #include <stdio.h> #include <string.h> #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; }
There is no limit to the number of functions you can have in a program. The only practical limit is dependant upon the amount of memory you have available in order to load the compiled program, whether it has 4 functions or 4 trillion functions. If the program makes use of dynamic libraries, then the amount of available memory reduces accordingly.
The number '4' can be considered a palindrome because it is read the same whether forward or backward. It can become another palindrome when it is considered as 2x2.
Palindrome number is a number like 121 which remains the same when its digits are reversed. To find this number in a simple java program, just follow the below way. sum = 0; while(n>0) { r=n % 10; sum=concat(r); n=n / 10; } print r;
class test { public static void main(int num) { int num2=num; int rnum=0; while (num2>0) { int q=num2/10; int dig=num2%10; rnum = rnum*10+dig; num2=q; } if (rnum==num) System.out.println("Palindrome number"); else System.out.println("Not a Palindrome number"); } }
Is a number that reads the same whether you read the digits from left to right or from right to left.
This program only suits PHP. If you want a proper one try C program for it available on web <body> <?php if(isset($_POST['submit'])) { $text = $_POST['text']; $string = mysql_real_escape_string($text); $invert = strrev($string); if($string == $invert) { echo "<br>Given number is a palindrome!!"; } else { echo "<br>Given number is not a palindrome!!"; } } ?> <form method="post"> <input type="text" name="text" id="text" /> <input type="submit" name="submit" value="Submit" id="submit" onclick="<?php $_SERVER['PHP_SELF']; ?>" /> </form> </body>
Any number that is is a palindrome will always be a palindrome.
A palindrome number: If the reverse of the number is equal to the number itself, then it is said to be a palindrome number. For example: 11, 22 ,55, etc...
There is no number palindrome for 563. A number palindrome is a number which is the same number when the digits are taken in the reverse order. For example, 2002 is the number palindrome of 2002 as it reads the same no matter which way it is read. Whereas 563 when read in reverse is "365" which is not the same as "563". Therefore ,there is no number palindrome for 563.
#include <stdio.h> #include <string.h> #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; } #include <stdio.h> #include <string.h> #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; }
606 is a palindrome.
Numeric palindrome