In: Computer Science
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: You'll find your isPalindrome method helpful here. This method calls for an optimization loop.)
import java.util.Scanner;
public class LongestPalindrome {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Longest
Palindrome: "+findLongestPalindrome(sc));
}
// takes series of words from user
//returns longest palindrome among them
public static String findLongestPalindrome(Scanner sc)
{
String s="";
String longestPal="";
while(!s.equalsIgnoreCase("Quit")){
s=sc.nextLine();
if(isPalindrome(s)){
if(s.length()>longestPal.length())
longestPal=s;
}
}
return longestPal;
}
//checks given string is palindrome or not
public static boolean isPalindrome(String s) {
char aC[]=s.toCharArray();
int i=0,j=s.length()-1;
//iterating from the both
ends
while(i<j){
if(aC[i]!=aC[j]){
return false;
}
i++;j--;
}
return true;
}
}
Note : If you like my answer please rate and help me it is very Imp for me