Write a Program to check if String is a palindrome or not

27 June 2022

Write a Program to check if String is a palindrome or not


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get an input string from the user and then check whether it is a palindrome string or not.

 

Input

noon

Output

Palindrome

 

Input

Talent

Output

Not a Palindrome

 

C Program

#include<stdio.h>

#include<string.h>

int main()

{

    char str[10];

    int i, len = 0, flag = 0;

    printf("Enter a string: ");

    scanf("%s",str);

    len = strlen(str);

    for (i = 0; i < len / 2; i++) {

        if (str[i] == str[len - i - 1])

        flag++;

    }

    if (flag == i)

        printf("Palindrome");

    else

        printf("Not a palindrome");

    return 0;

 

}

 

 

C++ Program

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

    char str[10];

    int i, len = 0, flag = 0;

    cout<<"Enter a string: ";

    cin>>str;

    len = strlen(str);

    for (i = 0; i < len / 2; i++) {

        if (str[i] == str[len - i - 1])

        flag++;

    }

    if (flag == i)

        cout<<"Palindrome";

    else

        cout<<"Not a palindrome";

    return 0;

 

}

 

 

Java

import java.util.Scanner;

public class Main {

    static boolean isPalindrome(String str)

    {

        int i = 0, j = str.length() - 1;

        while (i < j)

        {

            if (str.charAt(i) != str.charAt(j))

                return false;

            i++;

            j--;

        }

        return true;

    }

    public static void main(String[] args)

    {

       Scanner sc = new Scanner(System.in);

                             System.out.print("Enter a string: ");

        String str = sc.nextLine();

        if (isPalindrome(str))

            System.out.print("Palindrome");

        else

            System.out.print("Not a palindrome");

    }

}

 

Python

str1 = input("Enter a string: ").upper()

if str1 == str1[::-1]:

  print("Palindrome")

else:

  print("Not a palindrome")


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !