-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTestPalindromicWord.java
More file actions
49 lines (41 loc) · 1.44 KB
/
TestPalindromicWord.java
File metadata and controls
49 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* A word that reads the same backward as forward is called a palindrome,
* e.g., "mom", "dad", "racecar", "madam", and "Radar" (case-insensitive).
*
* Write a program called TestPalindromicWord, that prompts user for a word
* and prints ""xxx" is|is not a palindrome".
*
* Hints: Read in a word and convert to lowercase via in.next().toLowercase().
*/
package javaexercises.keyboard;
import java.util.Scanner;
/**
*
* @author User
*/
public class TestPalindromicWord {
public static void main(String[] args) {
TestPalindromicWord aTestPalindromicWord = new TestPalindromicWord();
//aTestPalindromicWord.run("Madam");
//aTestPalindromicWord.run("raDar");
//aTestPalindromicWord.run("DaD");
//aTestPalindromicWord.run("maM");
Scanner in = new Scanner(System.in);
String word;
System.out.print("Please enter a word to test it for palindromic: ");
word = in.next();
aTestPalindromicWord.run(word);
}
private void run(String word)
{
String reverseWord = "";
for(int i = word.length() - 1; i >= 0 ; i--) {
reverseWord += word.toLowerCase().charAt(i);
}
if (word.toLowerCase().equals(reverseWord.toLowerCase())) {
System.out.printf("%1$s is a palindrome.\n", word);
} else {
System.out.printf("%1$s is not a palindrome.\n", word);
}
}
}