Check whether a string is PALINDROME or Not | faceprep

Program to check whether the given string is a palindrome or not is discussed here. A string is said to be a palindrome if the original string and the reverse of the string are the same.

palindrome or not string

Algorithm to check whether a string is a palindrome or not

  • Input the string. 
  • Find the reverse of the string.
  • If the reverse of the string is equal to the input string, then return true. Else, return false.

Program to check whether the given string is a palindrome or not 

/* C program to check if the given string is a palindrome or not */
#include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];

printf(“Enter the string : “);
gets(a);

strcpy(b, a); /* Copying input string */
strrev(b); /* Reversing the string */

if (strcmp(a, b) == 0) /* Comparing input string with the reverse string */
printf(“The string is a palindrome\n”);
else
printf(“The string is not t a palindrome\n”);

return 0;
}

/* C++ program to check if the given string is a palindrome or not */
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char a[100], b[100];

cout << “Enter the string : “;
cin >> a;

/* Reversing the string */
int i, n = strlen(a);
for(i = 0; i < n; i++)
{
b[n-1-i] = a[i];
}

if (strcmp(a, b) == 0) /* Comparing input string with the reverse string */
cout << “The string is a palindrome\n”;
else
cout << “The string is not a palindrome\n”;

return 0;
}

/* Java program to check whether a string is a palindrome or not */

import java.util.*;
public class Main
{
public static void main(String args[])
{
String a, b = “”;
Scanner s = new Scanner(System.in);
System.out.print(“Enter the string : “);
a = s.nextLine();
int n = a.length();
for(int i = n – 1; i >= 0; i–)
{
b = b + a.charAt(i);
}
if(a.equalsIgnoreCase(b))
{
System.out.println(“The string is a palindrome”);
}
else
{
System.out.println(“The string is not a palindrome”);
}
}
}

# Python program to check whether a string is a palindrome or not

string = input(“Enter the string:”)
if(string == string[::-1]):
print(“The string is a palindrome”)
else:
print(“The string is not a palindrome”)

Output:

string is a palindrome or not