In: Computer Science
JAVA CODE, BEGINERS; Please use comments to explain
For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word:
banana dresser grammar potato revive uneven assess
Write a program that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters as lowercase letters.
Spell.java:
import java.io.*;
import java.lang.*;
//import scanner
import java.util.Scanner;
class Spell
{
public static void main(String[]
args)
{
//create
scanner
Scanner
input_str = new Scanner(System.in);
//prompt the
user
System.out.println("\nEnter word to be tested.Enter quit to
end....");
//Reads first
word as Input from the User.
String
input_word = input_str.nextLine();
//Converts the
words into LowerCase
input_word =
input_word.toLowerCase();
//executes only
if the word is not "quit".
while(!input_word.equals("quit"))
{
//set the boolean varaiable as true initially
for the given input.
boolean hasProperty = true;
//using for loop, loop through all
characcters
for(int i=1;i<input_word.length();i++)
{
char front_char =
input_word.charAt(i);
char back_char =
input_word.charAt(input_word.length()-i);
//checking if the reversed
word and the origial word is equal or not
if(front_char !=
back_char)
hasProperty = false;
}
//if the property doesn't exist
if(!hasProperty)
System.out.println("\n" +
input_word + " does not have the property.");
//if the property exist
else
System.out.println("\n" +
input_word + " have the property.");
//read the next Input.
input_word = input_str.nextLine();
}
}
}
Output: