In: Computer Science
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 should allow the user to test your program independently and interactively using Command Line Interface (CLI).
*******The code must commented properly**********
********** You must permit the user to enter their own palindrome after the test in run******************
Here i am providing the answer. Hope it helps. please give me a like. it helps me a lot. If any queries please do comment. i will be happy in clarifying.
Java code for above problem
import java.util.*;
import java.io.*;
class Main
{
// tetsing main method
public static void main(String args[]) throws
IOException
{
// this following steps prints
results of strings.txt file
Scanner input=new Scanner(new
File("strings.txt"));
while(input.hasNext())
{
String
str=input.nextLine();
System.out.println(isPalindrome(str));
}
// the following lines of codes
takes input from user from command line and prints the result
input=new Scanner(System.in);
System.out.print("\nEnter a string:
");
String str=input.nextLine();
System.out.println(isPalindrome(str));
}
// method that returns the string saying the given
string is palindrome or not
public static String isPalindrome(String str)
{
if(isPalin(str,0,str.length()-1))
return
"\""+str+"\" is a palindrome";
return "\""+str+"\" is not a
palindrome";
}
// recursive method that says whether the given string
is palindrome or not
public static boolean isPalin(String str,int start,int
end)
{
if(start>=end)
return
true;
if(str.charAt(start)!=str.charAt(end))
return
false;
return
isPalin(str,start+1,end-1);
}
}
Sample output
if "strings.txt" has following data
abcd
aaa
madam
string
radar
holidays
enjoy
then after running the above code, output looks as follows:
Thank you. please upvote.