Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need some help understanding the \"behind the scenes\" part of my code. I had

ID: 3667448 • Letter: I

Question

I need some help understanding the "behind the scenes" part of my code. I had a TA help me with part of the code so I'm not entirely sure what some of it does.

Here are my questions (I know you should post multiple questions seperately, but these 2 go hand in hand and overall, this a pretty short question.)

1. Using the recursive method, write out the string that is being processed at each step if the word “radar” was entered. Do the same for “Straw arts”.

2. Write some simple code using the iterative method (using loops) to solve this same problem. (The problem being - Implement a program which uses a recursive method that determines if a string entered by the user is a palindrome. A string is palindrome if it is the same forward as it is backwards, such as “radar” or “Taco cat”.)

Here's the code:

import java.util.Scanner;

public class RecursivePalindrome {

   public static void main(String[] args)

   {

       // TODO Auto-generated method stub

       Scanner keyboard = new Scanner(System.in);

       System.out.println("Enter a word and I will determine if it is a palindrome");//Gets user input

       String word = keyboard.nextLine();

       String combined = word.replaceAll("[\W]", "");//matches non-word characters, and removes the spaces.

       combined = combined.toLowerCase();//makes the combined word all lowercase

       if(checkPalindrome(word))//calls palindrome method and sees if combined word is a palindrome

       {

System.out.println("The word " + word + " is a palindrome");

       }

else

{

System.out.println("The word " + word + " is not a palindrome");

}

   }

  

   private static boolean checkPalindrome(String word)

   {

       if(word.length() <= 1)//if length is less than or equal to 1 then the string is palindrome

       {

           return true;

       }

if(word.charAt(0) == word.charAt(word.length()-1))//checks the first and last char of the string

{

   //if true, then does the same thing with substring

   //returns only when the string is done checking

return checkPalindrome(word.substring(1, word.length()-1));

}

//if string doesn't pass check, it's not a palindrome

return false;

   }

}

Explanation / Answer

package com.chegg.test;

import java.util.Scanner;

public class RecursivePalindrome {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner keyboard = new Scanner(System.in);
       System.out
               .println("Enter a word and I will determine if it is a palindrome");// Gets
                                                                                   // user
                                                                                   // input
       String word = keyboard.nextLine();
       String combined = word.replaceAll("[\W]", "");// matches non-word
                                                       // characters, and
                                                       // removes the spaces.
       combined = combined.toLowerCase();// makes the combined word all
                                           // lowercase
       if (checkPalindrome(combined))// calls palindrome method and sees if
                                   // combined word is a palindrome
       {
           System.out.println("The word " + word + " is a palindrome");
       } else {
           System.out.println("The word " + word + " is not a palindrome");
       }
   }

   private static boolean checkPalindrome(String word) {
       if (word.length() <= 1)// if length is less than or equal to 1 then the
                               // string is palindrome
       {
           return true;
       }
       if (word.charAt(0) == word.charAt(word.length() - 1))// checks the first
                                                               // and last char
                                                               // of the string
       {
           // if true, then does the same thing with substring
           // returns only when the string is done checking
           return checkPalindrome(word.substring(1, word.length() - 1));
       }
       // if string doesn't pass check, it's not a palindrome
       return false;
   }
}

Output:

Enter a word and I will determine if it is a palindrome
Straw arts
The word Straw arts is a palindrome

package com.chegg.test;

import java.util.Scanner;

public class PalindromeTest {
   public static void main(String args[])
   {
   String reverseString="";
   Scanner scanner = new Scanner(System.in);

   System.out.println("Enter a string to check if it is a palindrome:");
   String inputString = scanner.nextLine();
   String combined = inputString.replaceAll("[\W]", "");
   combined = combined.toLowerCase();
  
   int length = combined.length();

   for ( int i = length - 1 ; i >= 0 ; i-- )
   reverseString = reverseString + combined.charAt(i);

   if (combined.equals(reverseString))
   System.out.println("Input string is a palindrome.");
   else
   System.out.println("Input string is not a palindrome.");

   }
   }

Output:

Enter a string to check if it is a palindrome:
Taco cat
Input string is a palindrome.