Check if both halves of a string are Palindrome or not | Java Program to check whether both halves of a string is Palindrome or not ; Function to check and return if both halves of a string are palindromic or not ; Length of string ; Initialize both part as true ; If first half is not palindrome ; If second half is not palindrome ; If both halves are Palindrome ; Driver Code
public class Main { public static void checkPalindrome ( String S ) { int N = S . length ( ) ; boolean first_half = true ; boolean second_half = true ; int cnt = ( N / 2 ) - 1 ; for ( int i = 0 ; i < ( N / 2 ) ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( cnt ) ) { first_half = false ; break ; } if ( S . charAt ( N / 2 + i ) != S . charAt ( N / 2 + cnt ) ) { second_half = false ; break ; } cnt -- ; } if ( first_half && second_half ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String S = " momdad " ; checkPalindrome ( S ) ; } }