Sub | C ++ implementation of the approach ; Function to return the count of sub - strings starting from startIndex that are also the prefixes of str ; Function to return the count of all possible sub - strings of str that are also the prefixes of str ; If current character is equal to the starting character of str ; Driver code ; Function Call
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int subStringsStartingHere ( string str , int n , int startIndex ) { int count = 0 , i = 1 ; while ( i <= n ) { if ( str . substr ( 0 , i ) == str . substr ( startIndex , i ) ) { count ++ ; } else break ; i ++ ; } return count ; } int countSubStrings ( string str , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) count += subStringsStartingHere ( str , n , i ) ; } return count ; } int main ( ) { string str = " abcda " ; int n = str . length ( ) ; cout << ( countSubStrings ( str , n ) ) ; }