Special words are those words which starts and ends with the same letter. [15]
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.
import java.util.*;
class Ab
{
String str1;
boolean bool;
char ch;
Scanner sc=new Scanner(System.in);
public void take()
{
System.out.print("\nEnter the word: ");
str1=sc.nextLine();
//if(palindrome(str1))
bool=palindrome(str1);
if(bool==true)
{
System.out.print("\nThe word: "+ str1 + " is palindrome.");
System.exit(0);
}
bool=special(str1);
if(bool==true)
System.out.print("\nThe word: "+ str1 + " is Special.");
else
System.out.print("\nThe word: "+ str1 + " is neither Special nor palindrime.");
}
private boolean palindrome(String s)
{
int i,x,len;
len=s.length();
x=len-1;
for(i=0;i<len/2;i++)
{
if(s.charAt(i)!=s.charAt(x))
break;
x--;
}
if(i<len/2)
return false;
else
return true;
}
private boolean special(String s)
{
int len;
len=s.length();
if(s.charAt(0)==s.charAt(len-1))
return true;
else
return false;
}
}
Sample Input Output
Enter the word: malayalam
The word: malayalam is palindrome.
The word: malayalam is palindrome.
Enter the word: cosmic
The word: cosmic is Special.
Enter the word: abcd
The word: abcd is neither Special nor palindrime.
No comments:
Post a Comment