Count of sub | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubStr ( string str , int n , char x ) { int res = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == x ) { res += ( ( count + 1 ) * ( n - i ) ) ; count = 0 ; } else count ++ ; } return res ; } int main ( ) { string str = " abcabc " ; int n = str . length ( ) ; char x = ' c ' ; cout << countSubStr ( str , n , x ) ; return 0 ; }