Skip to main content

Check Whether The String Is Palindrome Or Not

Welcome back to my blog! In my previous post, you learnt how to print a pattern in Java using Eclipse.
Today you will learn how to check whether a string from terminal is palindrome or not.

Let's get started!



Step #1. Open Eclipse and choose the directory where you want to save your work.




Step #2. Select File>New>Project as shown in the picture below:




Step #3. Select Java>Java  Project.

Then, click Next.



Step #4. Enter the name of the project in Project name.

Then click on Finish.



Step #5. You will see the name of your project in Package Explorer:




Step #6. You will find a folder named as "src" under the name of your project:




Step #7. Right click on the folder "src">New>Class as demonstrated below:




Step #8. Enter the name of the class as shown below:

Then click on Finish.



Step #9. You will see a screen like this:




Step #10. Write the following code:


import java.util.Scanner;
public class Pal {
          public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
System.out.println("Enter a string:" );
String Str = Scan.nextLine();
int i = Str.length();
          String Rev = "";
     for(i = i-1; i >= 0; i --)
          {
                    Rev = Rev + Str.charAt(i);
          }
if(Str.equals(Rev))
          {
               System.out.println("The string is palindrome!");
          }
          else
               {
                    System.out.println("The string is not palindrome.");
               }
             }
           }



Step #11. Run the code by clicking on the Run button as shown:




Step #12.  If the entered string is palindrome, like lol, madam, level, rotor, etc., you will see the output as follows:



If the entered string is not palindrome, you will see the output as follows:




Hurrah!!! You have learnt how to make a Java program which checks a string from terminal whether it is palindrome or not.
Feel like a programmer!!!

If you find this post helpful, please share it with your friends, family and colleagues as much as you can.
Thank you for reading!   :)

Comments

Popular posts from this blog

VLOOKUP in MS Excel

VLOOKUP in MS Excel H ello and welcome back to Learn Java. It's been very long since I wrote my last post. This post will give you a short and simple tutorial on using VLOOKUP in MS Excel. Verticle Lookup or VLOOKUP is a function in MS Excel that helps a user look for a specific value in a column   based on another particular value in the first column. A simple example: Your boss asks you to find the price of a food item based on its name. Syntax: VLOOKUP(the value you want to look up, where you want to look for it(array/column/table), the column number the value is in, return an approximate or exact match) Let's try to understand it more clearly: Here's a little data set containing a table of my favourite food items.  Step 1: Select a specific cell/text value you would like to search for in the given table/data set. Here, we are looking for the price of Pizza, so we use cell value "Pizza" which is also cell reference A3. We are making sure to use a cell value/re

Simple Java program

J ava is a high level programming language which is used almost everywhere- in Android mobile phones, set top boxes, in Google Maps, video games, etc. Today let's see how to make the simplest Java program- "Hello, world" in Eclipse. Step #1. Select your file location where you want to save your work. Step #2. Select File>New>Project as shown in the picture below: Step #3. Select Java>Java Project>Next as shown below: Step #4. Enter the project name in Project name as shown below: Step #5. Select Finish. Step #6. Select Yes. Step #7. Double click one the project name. You will see a folder named as "src" under the the project HelloWorld. Step #8. Right click on the folder "src">New>Class. Step #9. Enter the name of the class. Then click on Finish. Step #10. You will see a screen like this: Step #11. Write the following code: public class Hello { public static void main(String[] args) { System.out.println("Hello, world!!"); }