Count All Palindrome Sub | Java Program to count palindrome substring in a string ; Method which return count palindrome substring ; Iterate the loop twice ; Get each substring ; If length is greater than or equal to two Check for palindrome ; Use StringBuffer class to reverse the string ; Compare substring wih reverse of substring ; return the count ; Driver Code ; Declare and initialize the string ; Call the method
public class PalindromeSubstring { static int countPS ( String str ) { String temp = " " ; StringBuffer stf ; int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = i + 1 ; j <= str . length ( ) ; j ++ ) { temp = str . substring ( i , j ) ; if ( temp . length ( ) >= 2 ) { stf = new StringBuffer ( temp ) ; stf . reverse ( ) ; if ( stf . toString ( ) . compareTo ( temp ) == 0 ) count ++ ; } } } return count ; } public static void main ( String args [ ] ) throws Exception { String str = " abbaeae " ; System . out . println ( countPS ( str ) ) ; }