Count substrings that starts with character X and ends with character Y | function to count substrings starting with character X and ending with character Y ; to store total count of required substrings ; to store count of character ' x ' up to the point the string ' str ' has been traversed so far ; traverse ' str ' form left to right ; if true , increment ' count _ x ' ; if true accumulate ' count _ x ' to ' tot _ count ' ; required count ; Driver code
< ? php function countSubstr ( $ str , $ n , $ x , $ y ) { $ tot_count = 0 ; $ count_x = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == $ x ) $ count_x ++ ; if ( $ str [ $ i ] == $ y ) $ tot_count += $ count_x ; } return $ tot_count ; } $ str = " abbcaceghcak " ; $ n = strlen ( $ str ) ; $ x = ' a ' ; $ y = ' c ' ; echo " Count = "