Program opened below. You can scroll through the algorithm, Java code and output.

Word Palindrome

Download .java

Question

Accept a word and determine whether it is a palindrome.

Step 1: Start the program and accept a word. Step 2: Store the entered word in a string variable. Step 3: Initialise another string as empty to hold the reversed word. Step 4: Find the index of the last character of the word. Step 5: Visit the characters from the last index down to zero. Step 6: Append the current character to the reversed string. Step 7: Continue until every character has been copied in reverse order. Step 8: Compare the reversed word with the original word, ignoring case. Step 9: If both words are equal, display Palindrome. Step 10: Otherwise display Not a palindrome. Step 11: Stop the program.
Java
import java.util.*;
class WordPalindrome
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String s = sc.next();
        String rev = "";
        for (int i = s.length() - 1; i >= 0; i--)
        {
            rev += s.charAt(i);
        }
        if (s.equalsIgnoreCase(rev))
        {
            System.out.println("Palindrome");
        }
        else
        {
            System.out.println("Not a palindrome");
        }
    }
}
Input: madam Output: Palindrome
← Previous Program 42 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.