Daily Java Challenge #4 – Check if a String is a Palindrome (Beginner Level)

Check if a String is a Palindrome
  • Save
Check if a String is a Palindrome

🧠 Problem Statement

Write a Java program to check whether a word entered by the user is a palindrome or not.

πŸ“₯ Input

A word, like:
madam

πŸ“€ Output

A message like:
Yes, it's a palindrome!
or
No, it's not a palindrome.

πŸ’‘ What is a Palindrome?

A palindrome is a word that reads the same backward and forward.

Some examples:

🧾 Java Code

import java.util.Scanner;

public class PalindromeCheck {
    public static void main(String[] args) {
        // Step 1: Take input from the user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String input = scanner.nextLine();

        // Step 2: Reverse the word
        String reversed = "";
        for (int i = input.length() - 1; i >= 0; i--) {
            reversed += input.charAt(i);
        }

        // Step 3: Check if input equals reversed
        if (input.equals(reversed)) {
            System.out.println("Yes, it's a palindrome!");
        } else {
            System.out.println("No, it's not a palindrome.");
        }
    }
}

πŸ” Step-by-Step (Explained Super Simply)

  1. We ask the user to type a word.
  2. We reverse that word letter by letter.
  3. We compare the original and the reversed word.
  4. If both are the same β†’ it’s a palindrome.
  5. If not β†’ it’s not a palindrome.

πŸ“š Example

πŸ‘‰ Input: level
βœ… Output: Yes, it's a palindrome!

πŸ‘‰ Input: apple
❌ Output: No, it's not a palindrome.

🎯 Try This Mini Challenge

Can you do the same thing but ignore capital letters?
(Hint: Use .toLowerCase() before checking)

πŸ“ See you tomorrow with Challenge #5!
Keep coding! Even small programs like this build your confidence. πŸ’ͺ

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *