Answer:
import java.util.Scanner;
public class Pallindrome {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the input string: ");
String word = in.nextLine();
System.out.println(isPallindrome(word));
}
public static String isPallindrome(String word){
String rev ="";
int len = word.length();
for ( int i = len - 1; i >= 0; i-- )
rev = rev + word.charAt(i);
if (word.equals(rev))
return word+" is palindrome";
else
return word+ " is not palindrome";
}
}
Explanation:
- Create the method in Java to receive a String parameter
- Using a for loop reverse the string
- Use another for loop to compare the characters of the original string and the reversed string
- If they are equal print palindrome
- else print not palindrome
- Within the main method prompt user for a sentence
- Call the method and pass the sentence entered by user