[Solved] Detecting Palindromes String with Java

Detecting Palindromes: palindrome is a word, phrase, or sentence that reads the same backwards and forwards. For example, the word “radar” is a palindrome. For this problem, you must write a program that reads a string of characters and prints whether or not the entire string is a palindrome. Your program should ignore all blanks, numbers,  punctuation and special characters, and should be case insensitive (i.e., it should not care if characters are capital letters or small letters). For example “5R3a dAr!” should be considered to be a palindrome.

INPUT FORMAT

Input consists of a string which is no greater than 100 characters long.

OUTPUT FORMAT

The program should print  yes (if the input was a palindrome) or no (otherwise).

Sample Input 1

Radar

Sample Output 1

yes

Sample Input 2

rAd . Ar

Sample Output 2

yes

Sample Input 3

radiar

Sample Output 3

no

Solution

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        int i;
        Scanner sc = new Scanner(System.in);
        String oldString, newString = "", palindromeString = "";
        int asciivalue;
        //sauravhathi
        oldString = sc.nextLine();
        for (i = 0; i < oldString.length(); i++) {
            if (oldString.charAt(i) >= 'A' && oldString.charAt(i) <= 'Z') {
                asciivalue = (int) oldString.charAt(i) + 32;
                newString = newString + (char) asciivalue;
            } else if (oldString.charAt(i) >= 'a' && oldString.charAt(i) <= 'z') {
                newString = newString + oldString.charAt(i);
            }
        }
        for (i = newString.length() - 1; i >= 0; i--) {
            palindromeString = palindromeString + newString.charAt(i);
            //sauravhathi
        }
        if (newString.equals(palindromeString))
            System.out.print("yes");
        else
            System.out.print("no");
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *