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

Good evening guys. So in my assignment I had to write a recursive method to dete

ID: 3738390 • Letter: G

Question

Good evening guys. So in my assignment I had to write a recursive method to determine if a String is a palindrome.

Create a String array with several test cases and test your method. The code looks like this

public class Recursionn { static boolean recurse(String s, int i, int n) {
if (i == n / 2) {
if (s.charAt(i) == (s.charAt(n / 2))) {
return true;
} else {
return false;
}
}
if (s.charAt(i) == (s.charAt(n - i - 1))) {
return recurse(s, i + 1, n);  
}
return false;
}


public static void main(String[] args)throws Exception
{
Scanner sc = new Scanner(new File("input.txt"));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
for (int i = 0; i < lines.size(); ++i) {
if (recurse(lines.get(i), 0, lines.get(i).length()) == true) {
System.out.println(lines.get(i) + " is a palindrome");
} else {
System.out.println(lines.get(i) + " is not a palindrome");
}
}
}
}

Now I need to add a method to this code where the user to test your program independently interactively. So the method has to ask the user to input 5 strings, and the output has to be if its

palindrome or not. I am still hacing some issues with using various methods in one program. Please help and thanks for your time.

Explanation / Answer

StringPalindrome.java

import java.util.Scanner;

class Recursionn {

static boolean recurse(String s, int i, int n) {

if (i == n / 2) {

if (s.charAt(i) == (s.charAt(n / 2))) {

return true;

} else {

return false;

}

}

if (s.charAt(i) == (s.charAt(n - i - 1))) {

return recurse(s, i + 1, n);  

}

return false;

}

}

public class StringPalindrome {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String s[] = new String[5];

System.out.println("Enter the "+s.length+" Strings: ");

for(int i=0;i<s.length;i++) {

s[i]=scan.nextLine();

}

for(int i=0;i<s.length;i++) {

if(Recursionn.recurse(s[i], 0, s[i].length())) {

System.out.println("String "+s[i]+" is a palindrome");

} else {

System.out.println("String "+s[i]+" is NOT a palindrome");

}

}

}

}

Output:

Enter the 5 Strings:
aaaaaa
civil
madam
uncle
apple
String aaaaaa is a palindrome
String civil is NOT a palindrome
String madam is a palindrome
String uncle is NOT a palindrome
String apple is NOT a palindrome

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