-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome.java
More file actions
50 lines (47 loc) · 1.66 KB
/
Copy pathPalindrome.java
File metadata and controls
50 lines (47 loc) · 1.66 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
50
// Nancy
/* Write a method-- public boolean isPalindrome (String word) -- that tests
* whether word is a palindrome (the same when read forward or backward, as
* in “madam”). Test isPalindrome using a test program. Upgrade isPalindrome
* so that it can hand any phrase (as in “Madam, I’m Adam”). In testing for a
* palindrome, disregard all spaces, punctuation marks, apostrophes, and other
* non-alphanumeric characters and consider lower- and uppercase letters the
* same. Do not count an empty string as a palindrome. (Remember the character
* class has static methods boolean isLetterOrDigit (ch) and char
* toUpperCase(ch)!)
*/
import java.util.*;
public class Palindrome
{
public static void main (String [] args){
Scanner scan = new Scanner (System.in);
System.out.println("Please input a string");
String str = scan.nextLine();
str = str.toLowerCase();
str = str.trim();
int y = str.length();
int x = 0;
while(x < y){
if (Character.isLetterOrDigit(str.charAt(x)) == false){
str = str.substring(0, x) + str.substring (x + 1);
}
else{ x++;}
y = str.length();
}
y = str.length() - 1;
int z = 0;
boolean palindrome = true;
while (z <= y && palindrome == true){
if (str.charAt(z) != str.charAt(y)){
palindrome = false;
}
z++;
y--;
}
if (palindrome) {
System.out.println("Yes! That's a palindrome!");
}
else {
System.out.println("Not a palindrome.");
}
}
}