Pleas write a program that checks if any array of string is a plaindrome
required input:
isPalindrome("dog","dog","cat","cat") ---> false
isPalindrome("a","tree","tree","a" ---> true;
isPalindrome("a","b","train","b","a") ---> true
this needs to be written in java
Solution for the given question are as follows -
Code :
class ArrayPalindrome {
// checkPalindrome is a Recursive function check array is
Palindrome or Not
static int checkPalindrome(String arr[], int start, int end)
{
if (start >= end) {
return 1;
}
if (arr[start] == arr[end]) {
return checkPalindrome(arr, start +
1, end - 1);
}
else {
return 0;
}
}
public static void main (String[] args) {
String arr[] = { "dog","dog","cat","cat" };
// String arr[] = { "a","tree","tree","a" };
// call to checkPalindrome() function
int result = checkPalindrome(arr, 0, arr.length - 1);
if (result == 1)
System.out.print( "Array Is
Palindrome");
else
System.out.println( "Array Is Not
Palindrome");
}
}
Code Screen Shot :
Output :
Get Answers For Free
Most questions answered within 1 hours.