Special Words Are Those Words that Start and End with the Same Letter. Examples: Existence Comic Window Palindrome Words Are Those Words Which Read the Same from Left to Right
Advertisements
Question
Special words are those words that start and end 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 a special word.
Advertisements
Solution 1
import java.util.Scanner;
class test
{
public static void main ()
{
Scanner s = new Scanner (System.in);
System.out.println ("Enter a word");
String w = s.next ();
int I = w.length ();
string w1 = ""; char ch1, ch2;
for (int k = 0; k < 1; k++)
{
ch1 = w.charAt (k);
w1 = chI + w1:
}
if (w1. equals (w) == true)
System.out.println ("It is palindrome word");
else if (w.charAt (0) == w.charAt (I - 1))
System.out.println ("It is only a special word");
else
System.out.println ("It is not a special word");
}
}
Solution 2
// program to accept a word and print whether it is a special word or palindrome
import java.util.*;
public class SQ18
{ public static void main(String j[])
{ int i;
String rev = "", s;
System.out.println("Enter a WORD");
Scanner obj = new Scanner(System.in);
s = obj.next().trim(); // delete leading/trailing spaces in the input
for (i = s.length() − 1; i >= 0; i--) // decrement
rev = rev + s.charAt(i); // reverse of the word
if (s.equals(rev))
System.out.println("Palindrome word");
else if (s.charAt(0) == s.charAt(s.length() − 1)) // first and last character
System.out.println("Special word");
else
System.out.println("Not a special word or a Palindrome");
}}