Answer on Question #47578, Programming, Java | JSP | JSF
Problem.
Write a program to let user enter a sequence of numbers, then the program will find the longest palindromic substring, and visualize it using ascii characters.
Solution.
Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Input
Scanner s = new Scanner(System.in);
String input = s.nextLine();
String maxString = "";
// Find the largest palindromic substring
for (int i = 0; i < input.length(); i++) {
for (int j = i; j < input.length(); j++) {
String subString = input.substring(i, j);
if (subString.equals(new StringBuilder(subString).reverse().toString())) {
if (maxString.length() < subString.length()) {
maxString = subString;
}
}
}
}
// Output
System.out.println(maxString);
}
}Result
1234121214121223457
212141212