0% found this document useful (0 votes)
42 views

Three Palindrome Program: Submitted By, Mohan Raj M CSE-A 3rd Year Program

The document describes a Java program that takes a string as input and checks if it contains 3 palindromic substrings, outputting the 3 substrings if it does and "impossible" if not, using recursive methods to check for palindromes and nested loops to check all substring combinations. It was submitted by Mohan Raj M, a 3rd year CSE student, and includes the program code and an example output.

Uploaded by

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

Three Palindrome Program: Submitted By, Mohan Raj M CSE-A 3rd Year Program

The document describes a Java program that takes a string as input and checks if it contains 3 palindromic substrings, outputting the 3 substrings if it does and "impossible" if not, using recursive methods to check for palindromes and nested loops to check all substring combinations. It was submitted by Mohan Raj M, a 3rd year CSE student, and includes the program code and an example output.

Uploaded by

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

Three Palindrome Program :

Submitted By,

Mohan Raj M
CSE-A 3rd Year

Program:

import java.util.Scanner;

public class Main


{
public static void main(String args[])
{
String str,stren="";
int n,i,j,k;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
n=str.length();
for(i=1;i<n-2;i++){
if(palindrome(str.substring(0,i))){

for(j=i+1;j<n;j++){

if(palindrome(str.substring(i,j))&&palindrome(str.substring(j))){
System.out.println(str.substring(0,i));
System.out.println(str.substring(i,j));
System.out.println(str.substring(j));
System.exit(0);
}
}
}
}
System.out.println("impossible");
}
public static boolean palindrome(String str){
int length = str.length();
String rev = "";
for ( int i = length - 1; i >= 0; i-- )
rev = rev + str.charAt(i);
if (str.equals(rev))
return true;

return false;
}

Output :
Trace :

You might also like