If you are searching for a program for Palindrome in c , c++ and java you are at the right place. Read the complete article.
What is a palindrome?
Palindrome is a string or number, if we read that string or number from backwards then it is same as if we read it from forward.
Example:
- madam
- aabaa
- yytyy
- abaaba
- 121
- 122221
How to check a string is palindrome?
Now lets check various methods of checking for palindrome.
- By reversing the string.
- By comparing the characters.
- Converting a number to string then either reversing or comparing character index.
- Reversing that number and comaring.
Palindrome in C
Lets use reversing a string to write palindrome program in c using pre-defined strrev( ) function.
//palindrome program in c
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char string[100];
//Input a string
printf("Palindrome in c\n Enter a string:");
scanf( "%s",string );
//Creating another reverse string for comaring
char temp_string[100];
int length=strlen(string);
temp_string[length]='\0';
//copy input string
strcpy( temp_string,string );
//reverse input string
strrev(string);
//compare both string
if( strcmp( string,temp_string )==0 )
{
printf("\nString is a palindrome\n" );
}
else
{
printf("\nString is not a palindrome\n" );
}
return 0;
}
Palindrome in C without using string functions
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<math.h>
int main(int argc, char const *argv[])
{
char string[100];
//Input a string
printf("Palindrome in c\n Enter a string:");
scanf( "%s",string );
//Now lets compare character by character.
int length=strlen(string);
//find middle element
int middle_element=floor(length/2);
int j=length-1;
//start comparing by index
for( int i=0 ; i<=middle_element ; i++ )
{
if( string[i]!=string[j] )
{
printf("\nString is not a palindrome\n" );
return 0;
}
j--;
}
printf("\nString is a palindrome\n" );
return 0;
}
Palindrome number program in C
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<math.h>
int main(int argc, char const *argv[])
{
int number=0;
//Input a string
printf("Palindrome in c\n Enter a number:");
scanf( "%d",&number );
int temp=number;
int reverse_number=0;
while( temp>0 )
{
reverse_number=reverse_number*10;
reverse_number=reverse_number+( temp%10 );
temp=temp/10;
}
if( reverse_number==number )
{
printf("\nNumber is a palindrome\n" );
}
else
{
printf("\nNumber is not a palindrome\n" );
}
return 0;
}
Palindrome program in C++
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
string str;
//input a string
cout<<"Enter a string"<<endl;
cin>>str;
string reverse_string;
//lets reverse a string
reverse_string=str;
reverse(reverse_string.begin(),reverse_string.end());
if( str==reverse_string )
{
cout<<"String is a palindrome"<<endl;
}
else
{
cout<<"String is not a palindrome"<<endl;
}
return 0;
}
Palindrome in C++ character comparisn method
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
string str;
//input a string
cout<<"Palindrome in c++\nEnter a string:"<<endl;
cin>>str;
//find length of string
int middle_element=floor(str.size()/2);
int j=str.size()-1;
//start comparing by index
for( int i=0 ; i<=middle_element ; i++ )
{
if( str[i]!=str[j] )
{
cout<<"String is not a palindrome"<<endl;
return 0;
}
j--;
}
cout<<"String is a palindrome"<<endl;
return 0;
}
Palindrome number program in C++
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
int number=0;
//Input a string
cout<<"Palindrome number in c++\n Enter a number:"<<endl;
cin>>number;
int temp=number;
int reverse_number=0;
while( temp>0 )
{
reverse_number=reverse_number*10;
reverse_number=reverse_number+( temp%10 );
temp=temp/10;
}
if( reverse_number==number )
{
cout<<"Number is a palindrome"<<endl;
}
else
{
cout<<"Number is not a palindrome"<<endl;
}
return 0;
}
Palindrome in java by string reverse method
package palindrome;
import java.util.Scanner;
public class palindrome {
public static void main(String[] args) {
//read a string
Scanner stdin=new Scanner(System.in);
System.out.println("Enter a string");
String str=stdin.nextLine();
String rev_str="";
//reverse a string
for( int i=str.length()-1 ; i>=0 ; i-- )
{
rev_str=rev_str+str.charAt(i);
}
//compare both strings
if( str.compareTo(rev_str)==0 )
{
System.out.println("String is a palindrome.");
}
else
{
System.out.println("String is not palindrome.");
}
}
}
Palindrome in java by Character comparisn method
package palindrome;
import java.util.Scanner;
public class palindrome {
public static void main(String[] args) {
//read a string
Scanner stdin=new Scanner(System.in);
System.out.println("Enter a string");
String str=stdin.nextLine();
int j=str.length()-1;
//compare index
boolean flag=true;
for( int i=0 ; i<Math.floor((j/2)) ; i++ )
{
if( str.charAt(i)!=str.charAt(j) )
{
flag=false;
break;
}
j--;
}
if( flag )
{
System.out.println("String is a palindrome.");
}
else
{
System.out.println("String is not palindrome.");
}
}
}
Palindrome number in java
package palindrome;
import java.util.Scanner;
public class palindrome {
public static void main(String[] args) {
//read a string
Scanner stdin=new Scanner(System.in);
System.out.println("Enter a number");
int number=stdin.nextInt();
int rev=0;
int temp=number;
while(temp>0)
{
rev=rev*10;
rev+=temp%10;
temp=temp/10;
}
if( rev==number )
{
System.out.println("Number is a palindrome.");
}
else
{
System.out.println("Number is not palindrome.");
}
}
}
People also practice
- Armstrong number in C | C++ and Java
- Linear Search in C | C++ and Java
- Binary Search Algorithm
- Factorial program in C | C++ and Java
- Odd even program in c | c++ and java
- Bubble sort in c | c++ and java
- What is a palindrome?