A palindrome is a word or phrase that is identical forward o
A palindrome is a word or phrase that is identical forward or backward, such as the word “racecar.” A standard palindrome is similar to a perfect palindrome, except that spaces and punctuation are ignored in a standard palindrome. For example, “Madam, I’m Adam” is a standard palindrome because the characters are identical forward or backward, provided you remove the spaces and punctuation marks.
Write a script that checks a word or a phrase (stored in a string variable) to determine if it is a standard palindrome, a perfect palindrome, or not a palindrome at all. Also, for each letter that the word/phrase contains, count and print the number of times that each consonant and vowel is encountered. Your output will look as follows:
Word/phrase: racecar
Perfect palindrome
Contains consonants:
r - 2
c - 2
Contains vowels:
a - 2
e - 1
Solution
Please find the below solution:
package org.learning;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PalindromeCheck {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(\"Input you string: \");
String input = reader.readLine();
reader.close();
PalindromeCheck check = new PalindromeCheck();
String formattedInput = check.removePunctuationAndSpaces(input).trim();
boolean mightBeStandardPalindrome = !(input.length() == formattedInput.length());
boolean isPalindrome = check.isPalindrome(formattedInput);
if(mightBeStandardPalindrome && isPalindrome){
System.out.println(\"Standard Palindrome\");
check.listChars(formattedInput);
}else if(isPalindrome){
System.out.println(\"Perfect Palindrome\");
check.listChars(formattedInput);
} else{
System.out.println(\"Not a Palindrome\");
}
}
String removePunctuationAndSpaces(String input){
char [] arr = input.toCharArray();
char [] res = new char [input.length()];
String patternString = \"[a-zA-Z]\";
Pattern pattern = Pattern.compile(patternString);
int count = 0;
for(char c: arr){
Matcher matcher = pattern.matcher(c+\"\");
if(matcher.matches()){
res[count] = c;
count++;
}
}
return new String(res);
}
void listChars(String input){
Map<Character, Integer> cmap = new HashMap<>();
Map<Character, Integer> vmap = new HashMap<>();
String vpatternString = \"[aeiouAEIOU]\";
Pattern vpattern = Pattern.compile(vpatternString);
for(char c: input.toCharArray()){
Matcher matcher = vpattern.matcher(c+\"\");
boolean isVowel = matcher.matches();
if(isVowel){
if(vmap.get(c) == null){
vmap.put(c, 1);
}else{
int count = vmap.get(c).intValue();
count++;
vmap.put(c, count);
}
}else{
if(cmap.get(c) == null){
cmap.put(c, 1);
}else{
int count = cmap.get(c).intValue();
count++;
cmap.put(c, count);
}
}
}
System.out.println(\"Consonants\");
for(Character c: cmap.keySet()){
System.out.println(c+\" : \"+cmap.get(c));
}
System.out.println(\"Vowels\");
for(Character c: vmap.keySet()){
System.out.println(c+\" : \"+vmap.get(c));
}
}
boolean isPalindrome(String input) {
int len = input.length();
for (int count = 0; count < (len / 2); ++count) {
if (input.charAt(count) != input.charAt(len - count - 1)) {
return false;
}
}
return true;
}
}




