Lab Assignment #5 :: CPTR 105 :: 04/17/09

Due by 12.30 pm today

[Programming Project #9]

For all of the following words, if you move the first letter to the end of the word, and then spell the word backwards, you will get the original word:

    banana dresser grammar potato revive uneven assess

Write a program that reads a word and determines if it has this property.

Continue reading and testing words until you encounter the word quit.

Solution

Scanner kb = new Scanner(System.in);

String inputString = kb.nextLine(); // read the first word
while(!inputString.equals("quit")) { // check if word is not "quit"
    boolean hasProperty = true; // set the boolean variable as true initially for this word
    for (int i = 1; i < inputString.length(); i++) { // loop through all characters 
        char frontChar = inputString.charAt(i); 
        char backChar = inputString.charAt(inputString.length() - i);
        if (frontChar != backChar)
            hasProperty = false;
    }
    if(!hasProperty)
        System.out.println(inputString + " does not have the quality");
    else
        System.out.println(inputString + " has the quality");

    inputString = kb.nextLine();
}