×

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

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

Python Knapsack problem

Python Knapsack problem Before we dig down about Knapsack problems in Python, first let's have a look at what is actually a knapsack problem. What is a knapsack problem? A problem from the...

5 minutes read.

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

String indices must be integers in Python

Lists, tuples, and strings are examples of iterable objects in Python whose items or characters can be retrieved by their index numbers. For instance, you might take the following action to...

3 minutes read.

How to convert integer to float in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

5 minutes read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

Python Argmin

Introduction The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken...

3 minutes read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Python Breakpoint

Introduction In Python 3.7, a brand-new created function called breakpoint() was added. Due to the close relationship between both the executable and the code of a debugging component, debugging Python programming...

4 minutes read.

How to Open a file in python with Path

In this tutorial, we will see rather than the traditional way of opening a file by locating or navigating it from our desktops; we will learn how to open the...

2 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

Find key from value in dictionary python

Python: Python programming language is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a...

5 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.