Count of sub | Function to return the count of required sub - strings ; Number of sub - strings from position of current x to the end of str ; To store the number of characters before x ; Driver code
< ? php function countSubStr ( $ str , $ n , $ x ) { $ res = 0 ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == $ x ) { $ res += ( ( $ count + 1 ) * ( $ n - $ i ) ) ; $ count = 0 ; } else $ count ++ ; } return $ res ; } $ str = " abcabc " ; $ n = strlen ( $ str ) ; $ x = ' c ' ; echo countSubStr ( $ str , $ n , $ x ) ; ? >