In: Computer Science
JAVA Arrays
4 Write a method called isPalindrome that takes a String as
input and returns true if the String is a palindrome.
import java.util.Scanner; public class Palindrome { public static boolean isPalindrome(String str) { String modified = ""; String reverse = ""; for (int i = 0; i < str.length(); ++i) { if (Character.isAlphabetic(str.charAt(i))) { modified += str.charAt(i); reverse = str.charAt(i) + reverse; } } return modified.equalsIgnoreCase(reverse); } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a string: "); String word = in.nextLine(); if (isPalindrome(word)) { System.out.println(word + " is a palindrome"); } else { System.out.println(word + " is NOT a palindrome"); } } }