Check given string is palindrome or not

Problem Statement:- Program to check whether a string is a palindrome or not.

Definition of Palindrome:- A word, phrase, or sequence that reads the same backward as forwards e.g. noon.


Sample Input/Output:-


Sample Input First:

madam

Sample Output First: 

Input string is palindrome

Sample Input Second: 

csinfo

Sample Output Second: 

Input string is not palindrome


Data requirement:-

   Input Data:- str

  Output Data:- String output

  Additional Data:- i, count, j

Program in C

Here is the source code of the C Program to Check given string is palindrome or not.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i,count=0;
    int j=strlen(str)-1;
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]==str[j])
            count++;
        j--;
    }
    if(count==strlen(str))
    {
        printf("Input string is palindrome");
    }
    else
        printf("Input string is not palindrome");
}

Input/Output:
Enter your String:madam
Input string is palindrome

Program in C++

Here is the source code of the C++ Program to check entered string is palindrome or not.

Code:

#include<iostream>
using namespace std;
int main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int len=0;
    while(str[len]!='\0')
    {
        len++;
    }
    int i,count=0;
    int j=len-1;
    for(i=0;i<len;i++)
    {
        if(str[i]==str[j])
            count++;
        j--;
    }
    if(count==len)
    {
        cout<<"Input string is palindrome";
    }
    else
        cout<<"Input string is not palindrome";
}

Input/Output:
Enter your String:csinfo
Input string is not palindrome

Program in Java

Here is the source code of the Java Program to Check given string is palindrome or not.

Code:

import java.util.Scanner;
public class p19 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
int i,count=0;
    int j=str.length-1;
for(i=0;i<str.length;i++)
    {
if(str[i]==str[j])
            count++;
        j--;
    }
if(count==str.length)
    {
System.out.println("Input string is palindrome");
    }
    else
    System.out.println("Input string is not palindrome");
cs.close();
}
}

Input/Output:
Enter your String:
csinfo360.com
Input string is not palindrome

Program in Python

Here is the source code of the Python Program to Check given string is palindrome or not.

Code:

str=input("Enter the String:")
count = 0
j=len(str)-1
for i in range(len(str)):
    if str[i]==str[j]:
        count+=1
    j-=1
if count==len(str):
    print("Input string is palindrome")
else:
    print("Input string is not palindrome")

Input/Output:
Enter the String:aba
Input string is palindrome

Post a Comment

0 Comments