Count of 1 | C ++ implementation of the approach ; Function to return the count of required characters ; While there are characters left ; Single bit character ; Two - bit character ; Update the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countChars ( string str , int n ) { int i = 0 , cnt = 0 ; while ( i < n ) { if ( str [ i ] == '0' ) i ++ ; else i += 2 ; cnt ++ ; } return cnt ; } int main ( ) { string str = "11010" ; int n = str . length ( ) ; cout << countChars ( str , n ) ; return 0 ; }