Coders Packet

A Java program to check whether a given string is palindrome or not for a given condition

By Vimmi Singh

satisfying a given condition and checking whether a given condition is palindrome or not in Java language

import java.util.Scanner;
public class PalindromeString {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String rev="";
System.out.println("Enter the word");
String s=sc.nextLine();
if(s.matches("^[A-Za-z]*$")) //( checking the condition that the string contains only alphabets using regex)

{
for(int j=s.length()-1;j>=0;j--) {
rev=rev+s.charAt(j);
}

if(s.equalsIgnoreCase(rev)) {
System.out.println(s+ " is palindrome");
}else
System.out.println(s+ " is not palindrome");

}else
System.out.println("Invalid input");

}
}

Here first we take a string input from user then we checked the condition that it contains only alphabets using regular expression, if true then we found the reverse of the string and compare original string with reversed string ignoring their case by using equalsIgonreCase if it is equal then we eill print string is palindrome else we will print string is not palindrome. And if the first if statement is false that is if string does not contain only alphabets then we will print Invalid Input.

Download project

Reviews Report

Submitted by Vimmi Singh (VimmiSingh)

Download packets of source code on Coders Packet