Count of substrings of a Binary string containing only 1 s | C ++ implementation to find count of substring containing only ones ; Function to find the total number of substring having only ones ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfSubstringWithOnlyOnes ( string s ) { int res = 0 , count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { count = s [ i ] == '1' ? count + 1 : 0 ; res = ( res + count ) ; } return res ; } int main ( ) { string s = "0110111" ; cout << countOfSubstringWithOnlyOnes ( s ) << endl ; return 0 ; }