Question

In: Computer Science

Write a recursive method to determine if a String is a palindrome. Create a String array...

Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method.

Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java

Solutions

Expert Solution

Code:

import java.util.*;


class Main {

      public static boolean isPalindrome(String s)
      {   // if length is 0 or 1 then String is palindrome

          if(s.length() == 0 || s.length() == 1)
              return true; 

          if(s.charAt(0) == s.charAt(s.length()-1))
          /* check for first and last char of String:
           * if they are same then do the same thing for a substring
           * with first and last char removed. and carry on this
           * until our string completes or condition fails
           */

          return isPalindrome(s.substring(1, s.length()-1));
  
          /* If program control reaches to this statement it means
           * the String is not palindrome hence return false.
           */
          return false;
      }
    public static void main(String[] args) {
        //String array with test cases
        String[] arr = {"hello","racecar","malyalam","nothing"};

        for(int i = 0;i<arr.length;++i){
            if(isPalindrome(arr[i])){
                System.out.println(arr[i] + " is palindrome");
            }else{
                System.out.println(arr[i] + " is not palindrome");
            }
        }
    }
}


output:

code screenshot:


Related Solutions

Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT