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
Examples:
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:
import java.util.Scanner;
public class Year2016Q1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = sc.next();
str = str.toUpperCase();
int len = str.length();
if (str.charAt(0) == str.charAt(len - 1))
{
boolean isPalin = true;
for (int i=1;i<len/2;i++)
{
if (str.charAt(i)!=str.charAt(len-1-i))
{
isPalin = false;
break;
}
}
if (isPalin)
{
System.out.println("Palindrome");
}
else
{
System.out.println("Special");
}
}
else
{
System.out.println("Neither Special nor Palindrome");
}
}
}
Output:
No comments:
Post a Comment