Given a binary string , count number of substrings that start and end with 1. | A simple C ++ program to count number of substrings starting and ending with 1 ; Initialize result ; Pick a starting point ; Search for all possible ending point ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int countSubStr ( char str [ ] ) { int res = 0 ; for ( int i = 0 ; str [ i ] != ' \0' ; i ++ ) { if ( str [ i ] == '1' ) { for ( int j = i + 1 ; str [ j ] != ' \0' ; j ++ ) if ( str [ j ] == '1' ) res ++ ; } } return res ; } int main ( ) { char str [ ] = "00100101" ; cout << countSubStr ( str ) ; return 0 ; }