Create a modified version of the program below so that the spaces, punctuation a
ID: 3760210 • Letter: C
Question
Create a modified version of the program below so that the spaces, punctuation and changes in uppercase and lowercase are not considered when determining wheter a string is a palindrome.
//********************************************************************
// PalindromeTester.java Author: Lewis/Loftus
//
// Demonstrates the use of nested while loops.
//********************************************************************
import java.util.Scanner;
public class PalindromeTester
{
//-----------------------------------------------------------------
// 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
{
System.out.println("Enter a potential palindrome:");
str = scan.nextLine();
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();
}
}
}
Explanation / Answer
Hey. You din't offered any testcases. Anyways, I solved thr problem for you, upto my understanding. If you need any further details, just revert.
Here, given a raw string, I'm just trying to move on, when a non numeric and non alphabetic character encounters, without any comparison.
The logic is here with:
//********************************************************************
// PalindromeTester.java Author: Lewis/Loftus
//
// Demonstrates the use of nested while loops.
//********************************************************************
import java.util.Scanner;
public class PalindromeTester
{
//-----------------------------------------------------------------
// 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
{
System.out.println("Enter a potential palindrome:");
str = scan.nextLine();
left = 0;
right = str.length() - 1;
String string;
while ( left < right)
{
//If the left character is neither alphabet nor digit, proceed.
if(!Character.isLetter(str.charAt(left)) && !Character.isDigit(str.charAt(left)))
{
left++;
continue;
}
//If the right character is neither alphabet nor digit, proceed.
if(!Character.isLetter(str.charAt(right)) && !Character.isDigit(str.charAt(right)))
{
System.out.println("0");
right--;
continue;
}
//If it is either digit or alphabet, then compare.
//If equal proceed.
if(str.charAt(left) == str.charAt(right))
{
left++;
right--;
continue;
}
//If not, conclude its not a palindrome.
break;
}
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();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.