Count All Palindrome Sub | C # 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 ; Swap values of l and r ; Driver Code ; Declare and initialize the string ; Call the method
using System ; class GFG { static int countPS ( String str ) { String temp = " " ; String 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 - i ) ; if ( temp . Length >= 2 ) { stf = temp ; stf = reverse ( temp ) ; if ( stf . ToString ( ) . CompareTo ( temp ) == 0 ) count ++ ; } } } return count ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String str = " abbaeae " ; Console . WriteLine ( countPS ( str ) ) ; } }