Special words are those words which starts and ends with the same letter.
Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and vice-versa.
Example:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC
All palindromes are special words, but all special words are not palindromes.
Write a program to accept a word check and print whether the word is a palindrome or only special word.
Solution
TC++ #7321
import java.util.Scanner;
public class Words {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter word: ");
String word = scanner.next();
// Check if word is palindrome
String reverse = "";
for (int i = word.length() - 1; i >= 0; i--) {
reverse = reverse + word.charAt(i);
}
if (word.equals(reverse)) {
System.out.println("Palindrome");
}
// Check if word is a special word
char firstLetter = word.charAt(0);
char lastLetter = word.charAt(word.length() - 1);
if (firstLetter == lastLetter) {
System.out.println("Special word");
}
}
}