-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPP409.java
More file actions
executable file
·71 lines (57 loc) · 2.09 KB
/
PP409.java
File metadata and controls
executable file
·71 lines (57 loc) · 2.09 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//********************************************************************
// PalindromeTester.java Java Foundations
//
// Demonstrates the use of nested while loops.
//********************************************************************
import java.util.Scanner;
public class PP409
{
//-----------------------------------------------------------------
// Tests strings to see if they are palindromes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
String strg, another = "y", str = "";
int left, right;
Scanner scan = new Scanner (System.in);
while (another.equalsIgnoreCase("y")) // allows y or Y
{
System.out.println ("Enter a potential palindrome:");
strg = scan.nextLine();
strg = strg.toLowerCase();
int currentLength = strg.length();
Scanner strscan = new Scanner(strg);
String token;
while(strscan.hasNext())
{
token = strscan.next();
System.out.println(token);
str = str + token;
}
System.out.println("Here it is: " + str + "\n");
for (int i=0; i < str.length(); i++)
{
System.out.println(str.charAt(i));
if (str.charAt(i) < 97 || str.charAt(i) > 122)
{
str = str.substring(0,i) + str.substring(i+1,str.length());
}
}
left = 0;
right = str.length() - 1;
while (str.charAt(left) == str.charAt(right) && left < right)
{
left++;
right--;
}
System.out.println();
if (left < right)
System.out.println ("That string is NOT a palindrome.");
else
System.out.println ("That string IS a palindrome.");
System.out.println();
System.out.print ("Test another palindrome (y/n)? ");
another = scan.nextLine();
}
}
}