InterviewSolution
| 1. |
Check if a given string is palindrome using recursion. |
|
Answer» /** Java program to CHECK if a given inputted string is palindrome or not using recursion.*/import java.util.*;public class InterviewBit { public static void main(String args[]) { Scanner s = new Scanner(System.in); String WORD = s.nextLine(); System.out.println("Is "+word+" palindrome? - "+isWordPalindrome(word)); } public static BOOLEAN isWordPalindrome(String word){ String reverseWord = getReverseWord(word); //if word equals its reverse, then it is a palindrome if(word.equals(reverseWord)){ return true; } return false; } public static String getReverseWord(String word){ if(word == NULL || word.isEmpty()){ return word; } return word.charAt(word.length()- 1) + getReverseWord(word.substring(0, word.length() - 1)); } } |
|