Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
71 lines (59 loc) · 2.39 KB

File metadata and controls

executable file
·
71 lines (59 loc) · 2.39 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//********************************************************************
// PalindromeTester2.java Java Foundations
//
// Demonstrates the use of nested while loops.
// Just like PalindromeTester but has better comments added.
//********************************************************************
import java.util.Scanner;
public class PalindromeTester2
{
//-----------------------------------------------------------------
// Tests strings to see if they are palindromes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
String str, another = "y";
int left, right;
Scanner scan = new Scanner (System.in);
while (another.equalsIgnoreCase("y")) // allows y or Y
{
int[] chars = new int[9999];
int count = 0;
System.out.println ("Enter a potential palindrome:");
str = scan.nextLine();
str = str.toLowerCase();
//find locations in the palindrome string with non alphabet characters and store their location in chars[] array.
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) < 97 || str.charAt(i) > 122)
{
chars[count] = i;
count++;
}
}
//starting with highest char position recorded in char[] array, remove each non alphabet character from the palindrome string.
for(int i = count - 1; i >= 0; i --)
{
str = str.substring(0,chars[i]) + str.substring(chars[i]+1,str.length());
}
//print out the palindrome string with all non alphabet characters removed. This is what will be tested to see if it is a palindrome.
System.out.println(str);
//Palindrome test
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();
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.