Program opened below. You can scroll through the algorithm, Java code and output.
Program 42
Word Palindrome
Question
Accept a word and determine whether it is a palindrome.
Algorithm
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 program
Javaimport 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");
}
}
}Sample output
Input: madam
Output: Palindrome