Question #47548

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. .

Expert's answer

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS