← Back to Java Projects
Java Palindrome
About This Project
This project checks whether a word entered by the user is a palindrome — a word that reads the same forwards and backwards (for example, racecar). It reads the string with the Scanner class, reverses it character by character, and compares the result.
Features
user_input()reads a string from the userCheckPalindrome()reverses the string using a loop- Prints the reversed string before checking
- Case-insensitive comparison with
equalsIgnoreCase() - Tells the user whether the string is a palindrome
Source Code
import java.util.Scanner;
class Palindrome
{
static String s;
void user_input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string to check: ");
s = sc.next();
}
boolean CheckPalindrome(String s)
{
String reversed = "";
for (int i = s.length() - 1; i >= 0; i--)
{
reversed = reversed + s.charAt(i);
}
System.out.println("Reversed string is: " + reversed);
return s.equalsIgnoreCase(reversed);
}
public static void main(String[] args)
{
Palindrome p = new Palindrome();
p.user_input();
if (p.CheckPalindrome(s))
{
System.out.println("It is a Palindrome");
}
else
{
System.out.println("It is Not a Palindrome");
}
}
}How It Works
- The program reads a string from the user with
Scanner - A loop walks backwards through the string, building the reversed version character by character
- The reversed string is printed to the console
equalsIgnoreCase()compares the original and reversed strings, ignoring letter case- If they match, it is a palindrome; otherwise it is not
Concepts Used
Scanner Input
Static Variables
String Manipulation
For Loops