How to check a string is palindrome or not in Jshell in Java 9?


JShell is the first REPL(Read-Evaluate-Print-Loop) interactive tool that has been introduced as a part of Java 9. It evaluates declarations, statements, and expressions as entered and immediately shows the results, and it runs from the command-line prompt.

Palindrome string is a string where it remains the same when reversed or word spelled the same way in both forward and backward directions.

In the below example, we can able to check whether the given string is palindrome or not in the JShell tool.

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> String str="LEVEL";
str ==> "LEVEL"

jshell> String revstring="";
revstring ==> ""

jshell> {
...>       for(int i=str.length()-1; i>=0; --i) {
...>          revstring +=str.charAt(i);
...>       }
...>       System.out.println(revstring);
...>       if(revstring.equalsIgnoreCase(str)){
...>          System.out.println("String is Palindrome");
...>       } else {
...>          System.out.println("String is not Palindrome");
...>       }
...>    }
LEVEL
String is Palindrome

raja
raja

e

Updated on: 01-Apr-2020

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements