Palindrome tester with Java, ignoring spaces and punctuation marks
I have a program built as long as it shouldn't ignore punctuation and spaces in a stream, and I was wondering if anyone could help me with the encoding for this? What I've tried doesn't seem to work. Here's what I have so far:
import java.util.Scanner;
public class PalindromeTester
{
public static void main (String[] args)
{
String str, another = "y";
int left, right;
char charLeft, charRight;
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 (left < right)
{
charLeft = str.charAt(left);
charRight = str.charAt(right);
if (charLeft == charRight)
{
left++;
right--;
}
else if (charLeft == ',' || charLeft == '.' ||
charLeft == '-' || charLeft == ':' ||
charLeft == ';' || charLeft == ' ')
left++;
else if (charRight == ',' || charRight == '.' ||
charRight == '-' || charRight == ':' ||
charRight == ';' || charRight == ' ')
right--;
else
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();
}
}
}
To clarify what Jim Garrison said, you need the following regex
String m = "Madam, I'm'',.,.'' Adam";
m = m.toLowerCase().replaceAll("\\W", "");
This will only leave letters and numbers and remove spaces and punctuation marks i.e. m will become "madamimadam" and you can run a regular palindrome test on that line.
You can read more about regular expressions here
source to share
This code for determining if a word is a palindrome can be much simplified. Find updated code
String word;
int z;
int y = 0;
int i = 0;
char letter;
Scanner input = new Scanner(System.in);
System.out.print("Enter a word: ");
word = input.nextLine();
word = word.replaceAll("\\s+", "");
word = word.toLowerCase();
z = word.length()-1;
while (i <= z){
if ((letter = word.charAt(i)) == (letter = word.charAt(z-i))){
y += 1;
}
i += 1;
}
if (y == (z+1)){
System.out.println("The word IS a palindrome");
}
else{
System.out.println("The word is NOT a palindrome");
}
}
}
source to share
This looks like a really old post, but I think I stumbled upon a simpler solution for the palindrome test. This checks for the first and last characters and moves in and out of the program as soon as the characters don't match.
public class CharTest {
public static void main(String[] args) {
//converts string to lowercase and replaces everything except numbers
// and alphabets
String s = "Niagara. O roar again!".toLowerCase().replaceAll("\\W", "");
int j=0;
int k = s.length() - 1;
while(j < s.length() / 2) { //loops until half the length of the string if
//even and floor value if odd.
if (s.charAt(j++) != s.charAt(k--)){//check for first and last chars
//and go inwards. if char do not match print 'Not a Palindrome' and exit
System.out.println("Not a Palindrome");
System.exit(0);}
}
System.out.println("Palindrome"); //if every chars match print "Palindrome"
}
}
source to share
You can greatly simplify your code by removing all spaces and punctuation before you start. Have a look at String.replaceAll (regex, replace). You must write a regular expression to match whitespace and punctuation, and provide an empty string ("") as a replacement. This will return a new line containing the original minus characters you want to ignore.
source to share
Have a look at the char entry documentation . In particular, the method isLetterOrDigit
. If this method returns false, then it denotes punctuation or space. There are other methods that can help with this.
This is a programming assignment from Java Software Solutions (PP3.11) that I assign to my students. Ironically, the teacher's solution uses Character.isLetterOrDigit(___)
(which is never mentioned in the book) and uses techniques to get rid of spaces and punctuation marks (without even learning the techniques at this point in the book), and char is not an official part of the AP CS subset. Silly publishers.
source to share