Palindrome Program with example
A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward as in the reverse order. The word "palindrome" was coined by the English writer Ben Jonson in the 17th century. It was derived from the Greek words palin, which means again and dromos, meaning direction.
Few commonly used palindrome words in day to day life are:
Racecar, Madam, pop, toot, eye, civic, level, peep and so on….
Racecar, Madam, pop, toot, eye, civic, level, peep and so on….
The longest commonly used word in English which is a palindrome is REDIVIDER. A chemical term DETARTRATED is two letters longer.
The Finnish language has a couple of 25-letter palindromic word:
SOLUTOMAATTIMITTAAMOTULOS which technically comprises of four words written in one. The word refers to a measurement laboratory for tomatoes.
The second being SAIPPUAKUPPINIPPUKAUPPIAS which means soap cup trader.
The Finnish language has a couple of 25-letter palindromic word:
SOLUTOMAATTIMITTAAMOTULOS which technically comprises of four words written in one. The word refers to a measurement laboratory for tomatoes.
The second being SAIPPUAKUPPINIPPUKAUPPIAS which means soap cup trader.
A palindrome is not necessarily a single word. It may be a phase comprising of two or more words. An example can be LIVE EVIL.
Types of Palindrome:
Word unit palindrome:
These are the palindromes in which the words form the same sentence in either directions.
Mirrored palindrome:
These palindromes are graphically reversible. The characters appear the same as in the reflection of a mirror except the order of reading them. This is possible when they contain the letters which have a symmetry about their vertical axis like A, H, I, M, O, T, U, V, W, X. A good example can be the expression “WOW”
Palindrome Programs:
The following statements illustrates the algorithm to find if a string entered by user is a Palindrome string or not…
Technique or Algorithm:
Step 1: Read string (as array of characters) from user
Step 2: Find the length l of the string entered above
Step 3: Run outer loop from 0th position to position l/2 of the array
Step 4: Run inner loop from (l-1)/2th position to starting position of the array
Step 5: Compare the characters at positions formed by outer loop with the locations of the inner loop of the character array (string)
Step 6: If equal characters are found, it is a Palindrome string, else it is not.
Step 2: Find the length l of the string entered above
Step 3: Run outer loop from 0th position to position l/2 of the array
Step 4: Run inner loop from (l-1)/2th position to starting position of the array
Step 5: Compare the characters at positions formed by outer loop with the locations of the inner loop of the character array (string)
Step 6: If equal characters are found, it is a Palindrome string, else it is not.
Program Code
The following is the implementation of the above algorithm.
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char A[20], B[20];
int l,i,j,x=0;
cout<<"\n\nEnter a line : ";
gets(A);
l=strlen(A);
for(i=0; i<=l/2 ;++i)
{
for(j=l-1;j>l/2;--j)
if(A[i]==A[j])
{
x=1;
break;
}
else
x=0;
}
if(x==1)
cout<<"It is a palindrome string";
else
coutx<<It is not a palindrome";
getch();
}
Now for numbers and digits…
With the help of the following algorithm you will be able to find if a number is a palindrome or not.
With the help of the following algorithm you will be able to find if a number is a palindrome or not.
Technique or Algorithm:
Step 1: Read a number from the user
Step 2: Store this number in another variable
Step 3: Reverse the original number using the program to reverse a number
Step 4: If the result (reversed number) is equal to the value stored in the new variable, it is a palindrome number, else its not.
Step 5: If equal characters are found, it is a Palindrome string, else it is not.
Step 2: Store this number in another variable
Step 3: Reverse the original number using the program to reverse a number
Step 4: If the result (reversed number) is equal to the value stored in the new variable, it is a palindrome number, else its not.
Step 5: If equal characters are found, it is a Palindrome string, else it is not.
Program Code
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int long a,n,r=0,s,x;
cout<<"Enter no of no.";
cin>>n;
for(int i=1; i<=n;i++)
{
cout<<"\n\nEnter the number : ";
cin>>a;
//reverse a number
x=a;
while(a>0)
{
s=a%10;
r=(r*10)+s;
a=a/10;
}
if(r==x)
cout<<x<<"is a palindrome";
else
coutx<<x<<"is not a palindrome";
}
getch();
}
I hope I am clear about Palindromes and the basic algorithm to implement it in a program to find if a string, number, phase is a palindrome or not.
Comment Below if you have any queries.
Comment Below if you have any queries.

Comments
Post a Comment