0% found this document useful (0 votes)
108 views1 page

Long Palindrome Text

This Java code finds the longest palindrome substring in a given string. It defines two methods - shorterpalin() which checks for palindromes of odd and even length around a center point, and longpalin() which calls shorterpalin() in a loop to find the longest palindrome by comparing lengths. The main() method takes user input, calls longpalin() to find the longest palindrome, and prints the output.

Uploaded by

sarathsnairxyz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views1 page

Long Palindrome Text

This Java code finds the longest palindrome substring in a given string. It defines two methods - shorterpalin() which checks for palindromes of odd and even length around a center point, and longpalin() which calls shorterpalin() in a loop to find the longest palindrome by comparing lengths. The main() method takes user input, calls longpalin() to find the longest palindrome, and prints the output.

Uploaded by

sarathsnairxyz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

*;
import java.io.*;
public class HelloWorld{

//code for smaller palins
//if the palin is odd it will check for the mid portion and //left and right are equal or not !
//if it is even it compares adjacent values and then checks for left and right are equal or not like
"##"
public static $tring shorterpalin%$tring s&int l&int r'
{
while%l()*++r,s.length%'++s.char-t%l'))s.char-t%r''
{
l..;
r//;
0
return s.substring%l/1&r';
0
public static $tring longpalin%$tring s'
{ $tring longest)s.substring%*&1';//assign smallest string to longest for checking
$tring palinstring;
//checking for the plaindromes in the given string and returns the longest
for%int i)*;i,s.length%'.1;i//'
{ //for odd palindromes
palinstring)shorterpalin%s&i&i';
if%palinstring.length%'(longest.length%''
longest)palinstring;
//for even palindromes
palinstring)shorterpalin%s&i&i/1';
if%palinstring.length%'(longest.length%''
longest)palinstring;
0
return longest;
0
public static void main%$tring 23args'{
$4stem.out.println%5enter the string 5';
$canner sin )new $canner%$4stem.in';
$tring input)sin.ne6t7ine%';
//8he longest plaindrome string
$tring longestpalin)longpalin%input';
$4stem.out.println%58he longest plaindrome string is 5/longestpalin';
0
0
sample input output9
enter the string :!;1<<<<1!<!
8he longest plaindrome string is 1<<<<1

You might also like