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

I created a recursive and iterative palindrome program that determines whether t

ID: 3817218 • Letter: I

Question

I created a recursive and iterative palindrome program that determines whether the string entered is a palindrome or not.

A semi-formal recursive definition of a palindrome

a) what is the recursive step

b) create appropriate comments for both the recursive and iterative programs.

recursive

import java.io.*;
public class RecursivePalindrome
{
static boolean CheckPalindrome(String s, int leftSide, int rightSide)
{
if (rightSide <= leftSide)
return true;
else if (s.charAt(leftSide) != s.charAt(rightSide))
return false;

else
return CheckPalindrome(s,leftSide+1,rightSide-1);

}
public static void main(String[] args) throws IOException
{

String str;
int n;   

InputStreamReader inStream = new InputStreamReader( System.in );
BufferedReader stdin = new BufferedReader( inStream );

System.out.print("Please enter any string: ");
str = stdin.readLine();

int lastPosition = str.length()-1;

boolean result = CheckPalindrome(str , 0, lastPosition);
if (result)

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

else   

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


}

}

iterative

import java.util.Scanner;
public class Iterative
{
public static void main(String args[]) {

Scanner reader=new Scanner(System.in);
System.out.println("please enter a string");
String input=reader.nextLine();

System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input));

System.out.println("please enter another string");
input=reader.nextLine();

System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input));

reader.close();

}
public static boolean isPalindrome(String input) {
if(input==null || input.isEmpty()) {
return true;
}

char[] array=input.toCharArray();
StringBuilder sb=new StringBuilder(input.length());
for(int i=input.length() -1; i>=0; i--) {
sb.append(array[i]);

}
String reverseOfString=sb.toString();
return input.equals(reverseOfString);

}
}

Explanation / Answer

Hi, I have commented required code.

please got through all comments.

please let me know in case of any issue.

Recursive step: CheckPalindrome(s,leftSide+1,rightSide-1);

import java.io.*;

public class RecursivePalindrome

{

   static boolean CheckPalindrome(String s, int leftSide, int rightSide)

   {

       if (rightSide <= leftSide) // if we have scanned all character(left and right pointer crossed each other)

           return true;

       // if current character form front and back is not equal then return false

       else if (s.charAt(leftSide) != s.charAt(rightSide))

           return false;

       else{

           // this is the recursive step

           // forwarding front pointer(i) and back pointer(j) by one step and

           return CheckPalindrome(s,leftSide+1,rightSide-1);

       }

   }

   public static void main(String[] args) throws IOException

   {

       String str;

       int n;

       InputStreamReader inStream = new InputStreamReader( System.in );

       BufferedReader stdin = new BufferedReader( inStream );

       System.out.print("Please enter any string: ");

       str = stdin.readLine();

       int lastPosition = str.length()-1;

       boolean result = CheckPalindrome(str , 0, lastPosition);

       if (result)

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

       else

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

   }

}

import java.util.Scanner;

public class Iterative

{

   public static void main(String args[]) {

       Scanner reader=new Scanner(System.in);

       System.out.println("please enter a string");

       String input=reader.nextLine();

       System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input));

       System.out.println("please enter another string");

       input=reader.nextLine();

       System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input));

       reader.close();

   }

  

   public static boolean isPalindrome(String input) {

      

       // Base case

       if(input==null || input.isEmpty()) {

           return true;

       }

      

       // getting character array of input string

       char[] array=input.toCharArray();

      

       // creating string builder object of lenght equalt to lenght of input string

       StringBuilder sb=new StringBuilder(input.length());

      

       // filling input character in string builder object

       for(int i=input.length() -1; i>=0; i--) {

           sb.append(array[i]);

       }

       // getting reverse of input string

       String reverseOfString=sb.toString();

      

       // if both are equal then input string is palindrome

       return input.equals(reverseOfString);

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote