Hi guys so I was playing with some code which would determine the whether a string is a palindrome or not. Its been a mixed bag of results with some palindromes being detected while others aren't. I do apologize to the lack of polish of my comments but I hope that someone might be able to see where the error is and give me good feedback. I think the error is in the set of code that removes the special characters from the string.
As a reference, I am using this webiste: http://www.palindromelist.net/ as the source for the palindrome strings. However the really long ones have resulted in errors.
package palindrome;
/*
* Author: venom
* Last Edit Date: 2/15/2019
* This program will check whether a string is a palindrome
* JDK 11
*/
import java.util.Scanner; // Scanner class used to collect user input of various strings
import javax.swing.JOptionPane; // Will be used to build the GUI for the palindrome test
public class Palindrome {
public static void main(String[] args) {
palindrome();
}
public static void palindrome(){
//Create the a scanner object to collect user input However will be re-enabled at the end
Scanner sc = new Scanner(System.in);
//System.out.print();
//Variable to store the user string
String input = null;
do {
input = JOptionPane.showInputDialog(null,"Welcome to the Palindrome Test App.\n" +
"Enter a string to test: ", "Palindrome App",
JOptionPane.INFORMATION_MESSAGE);
} while (input.isEmpty() == true);
input = input.toLowerCase();// set the input to lower case
// Here we are removing all special characters from a string
input = input.replace(" ","").replace("!", "").replace(".", "").replace(",", "").replace("?","")
.replace("-", "").replace("/", "").replace("\\","").replace("'", "").replace("\"", "").replace(":", "")
.replace("`", "").replace(";", "");
String message = null;
//String dummy1 = "Jabroni"; // This is the first dummy palindrome string
//dummy1 = dummy1.toLowerCase();
// String dummy2 = ""; // This a non palindrome string
int strLength = input.length(); // This will count the number of characters in a string
int midCount = strLength / 2; // The "midCount" is divided into half so that we do not loop all the way
// Initialize the character variables used to compare the chars in the loop
char bChar; // Will store to check the beginning character
char eChar; // Will store to check the ending char
// Initialize a counter variable which will be used to compare
//System.out.println(dummy1);
int cCount = 0; // This will count how many chars are similar
for (int x = 0; x < midCount; x++)
{
strLength--; // Reduces the string by 1
//System.out.println(strLength);
bChar = input.charAt(x);
eChar = input.charAt(strLength);
// If conditionals for comparison of characters
if (bChar == eChar){
cCount += 1;
}
}
// Compare the midpoint with cCount
// If they match then the code is a success if not we have failed
if (cCount == midCount){
message = "Success! ;-D The string is a Palindrome";
}
else
message = "It is not a Palindrome. Sorry :-(";
JOptionPane.showMessageDialog(null, message, "Palindrome App", JOptionPane.INFORMATION_MESSAGE);
}
}