Question

In: Computer Science

Write a recursive method that takes a String argument and recursively prints out each word in...

Write a recursive method that takes a String argument and recursively prints out each word in the String on a different line.

Note: You will need to use methods such as indexOf() and substring() within the String class to identify each word and the remaining string.

For example, if the input was “the cat purred”, then the method would print the following to the Java console:

the
cat
purred

Challenge Problem

Write a recursive method that takes a string as an input and returns a boolean value that tells you whether the string is a palindrome. A palindrome is a word/sentence that when read backwards is the same as the original word/sentence.

Some example palindromes: “civic”, “radar”, “deleveled”, “a man, a plan, a canal: Panama” (ignore the punctuation)

PreviousNext

Solutions

Expert Solution

Hi, Please find my implementation.

I have implemented both questions in same file as a function/method.

public class StringoOperation {

  

   public static void printWordRecursive(String str){

       // Base case

       if(str == null || str.trim().length() == 0)

           return;

      

       int index = str.indexOf(' ');

       // this is the last word

       if(index == -1){

           System.out.println(str);

           return;

       }else{

           // printing current word

           System.out.println(str.substring(0, index));

           // calling recursively

           printWordRecursive(str.substring(index+1));

       }

   }

  

   public static boolean isPalindrome(String str){

       if(str == null || str.trim().length() == 0)

           return false;

      

       int i=0;

       int j = str.length()-1;

      

       while(i < j){

           char ith = Character.toLowerCase(str.charAt(i));

           char jth = Character.toLowerCase(str.charAt(j));

          

           if(Character.isDigit(ith) || Character.isLetter(ith)){

               if(Character.isDigit(jth) || Character.isLetter(jth)){

                   //System.out.println(ith+" "+jth);

                   if(ith != jth) // not palindrome

                       return false;

                   i++;

                   j--;

                  

               }else{// skipping special characters

                   j--;

               }

           }else{ // skipping special characters

               i++;

           }

          

       }

      

       return true;

   }

   public static void main(String[] args) {

      

       printWordRecursive("This is a sample statemmets");

       System.out.println();

       System.out.println("Is 'madam' palindrome ? "+isPalindrome("madam"));

       System.out.println("Is 'mada mn' palindrome ? "+isPalindrome("mada mn"));

       System.out.println("Is 'deleveled' palindrome ? "+isPalindrome("deleveled"));

       System.out.println("Is 'a man, a plan, a canal: Panama' palindrome ? "+

       isPalindrome("a man, a plan, a canal: Panama"));

      

      

   }

}

/*

Sample run:

This

is

a

sample

statemmets

Is 'madam' palindrome ? true

Is 'mada mn' palindrome ? false

Is 'deleveled' palindrome ? true

Is 'a man, a plan, a canal: Panama' palindrome ? true

*/


Related Solutions

In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
IN JAVA A recursive method that takes a String as its argument and returns a list...
IN JAVA A recursive method that takes a String as its argument and returns a list of Strings which includes all anagrams of the String. This method will contain a loop that generates each possible substring consisting of all but one character in the input String, ie the substring that omits the first letter, then the substring that omits the second letter, etc. Within the loop, the method calls itself recursively for each substring. For each String in the list...
Write a program that prints out the characters of a string each on a line and...
Write a program that prints out the characters of a string each on a line and counts these characters.  (Do not use the length function len() ).
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 method in C# which takes string as an argument and extracts all words of...
Write a method in C# which takes string as an argument and extracts all words of length between 4 to 5 and contains vowels in it. Method returns an array of type string containing words which satisfied above criteria. Show these words in main().
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a function bracket_by_len that takes a word as an input argument and returns the word...
Write a function bracket_by_len that takes a word as an input argument and returns the word bracketed to indicate its length. Words less than five characters long are bracketed with << >>, words five to ten letters long are bracketed with (* *), and words over ten characters long are bracketed with /+ +/. Your function should require the calling function to provide as the first argument, space for the result, and as the third argument, the amount of space...
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
Write a Java method called printAvg that takes in two floating point numbers and prints out...
Write a Java method called printAvg that takes in two floating point numbers and prints out the average of them.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT