Introduction
In this blog, we will know whether a given string is
palindrome or not.
A palindrome is a word, phrase, number that can be read the same way in
either direction.
- import java.util.*;
- public class palindrom
- {
- public static void main(String arg[])
- {
- System.out.println("Provide a string to test");
- Scanner sc = new Scanner(System.in);
- String str1 = sc.next();
- String str2 = "";
- for (int i = str1.length() - 1; i >= 0; i--)
- str2 = str2 + str1.charAt(i);
- if (str1.equals(str2))
- System.out.println("Palindrom");
- else
- System.out.println("Not a palindrom");
- }
- }
Compile:-
javac palindrom.java
java palindrome
Ex: Provide a string to test:- BOB
Output:- String is palindrome