×

Find whether the given stringnumber is palindrome or not

Problem statement

Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to check whether the given string is palindrome or not. As a programmer, your task is to develop a code that takes a string input and checks whether it is palindrome or not.

Approach

If you know what a palindrome is, you can easily solve this question. A palindrome is a set of sequence of character, which looks similar from either side. It means that if you take a particular sequence and write it from left to right or from right to left, if both the written sequences are the same, then it is a palindrome.

You can solve this by reversing the given string and comparing it with the original string. If both the strings are the same, then you can return true or else false. For this, you need to declare an empty string and traverse the original string from the right and store each character into an empty string. After the completion of string traversal, you need to compare the original string with the new string and return the result.

Example 1.

import java.io.*;
import java.util.*;


public class Main
{
    public static void main (String args [])
    {
        Scanner sc1 = new Scanner (System.in);
        System.out.println("Enter the string to check whether it is palindrome or not:");
        String palindromeString = sc1.next();
// input the string
        String duplicateString = ""; // duplicate string
        int n = palindromeString.length(); // length of the string
        for (int i = n - 1; i >= 0; i--)
        {
            duplicateString = duplicateString + palindromeString.charAt (i); 
           // reversing the string
        }
          // comparing both string
        if (palindromeString.equalsIgnoreCase (duplicateString)
        {
            System.out.println("The given string"+palindromeString+" is palindrome.");
        }
        else
        {
            System.out.println("The string "+palindromeString+"is not palindrome.");
        }
    }
}

Output

Find whether the given string/number is palindrome or not
Find whether the given string/number is palindrome or not

Example 2: If input is in the number form.

import java.io.*;
import java.util.*;


class Main {
  public static void main (String [] args) {
      Scanner sc1= new Scanner (System.in);
      System.out.println("enter the number to check whether it is palindrome or not");
      int Palindromenumber = sc1.nextInt(); // input the integer 
      int revNum = 0, remainder = 0;
    
      // storing the input number to orgNum
      int orgNum = Palindromenumber;
    
      // getting thr reverse of orgNum
      // storing it to revNum
      while (Palindromenumber != 0) {
        remainder = Palindromenumber % 10 ; 
        revNum = revNum * 10 + remainder ;
        Palindromenumber /= 10 ;
      }
    
      // check if revNum and orgNumNum are equal
      if (orgNum == revNum) {
        System.out.println(orgNum + " is a Palindrome.");
      }
      else {
        System.out.println(orgNum + " is not a Palindrome.");
      }
  }
}

Output

Find whether the given string/number is palindrome or not
Find whether the given string/number is palindrome or not

Related Topics

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python format() function

Python format() function The format() function in Python formats a specified value into the given format. A ‘TypeErrorexception’ is raised if the method search reaches the object and the format_spec is non-empty, or if either the format_spec or the...

1 minute read.

How to Parse JSON in Python

JSON The JSON, the Java Script Object Notation, is an open standard file designed for data interchange; it is light-weighted text. The JSON uses human-readable text to store and transmit the...

4 minutes read.

Sklearn in Python

Scikit-learn or sklearn is a machine learning library used in Python that provides many unsupervised and supervised learning tools and algorithms. David Cournapeau first created it as a 2007 Google...

7 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

Python vs PHP

Python Python is a high-level, object-oriented, interpreted language that is used to create independent program and algorithms for a variety of applications. It has a large library support base. In 1990,...

3 minutes read.

Python EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.

Python's Qstandarditemmodel

Python More interactive and user-friendly than any other programming language is Python. Many libraries are used by the python programming language to speed up procedures. Python can be used to develop...

3 minutes read.

SKLearn Clustering

These are ml methods thatare responsible for detecting patterns and the similarities within the data.The clustering methods are unsupervised.Here the data is clustered to form groups with the help of...

3 minutes read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.