12345678910111213141516171819202122232425262728 |
- /* Write a function that returns the longest sequence
- * of consecutive zeroes in a binary string.
- */
- std::string longestZero(std::string str) {
- std::string count = "";
- std::string biggestCount = count;
- // 011000010110001
- // loop through each character in string
- for(int i=0;i<str.size();i++){
- // save each zero in the string to the end of count
- if(str[i] == '0'){
- count.append("0");
- }
- else if(str[i]=='1'){
- count = "";
- }
- // if the string is not a one or zero, this is not a binary string. program ABORTED!
- else {
- assert(false);
- }
- // compare each count to find the biggestCount
- if(count.size()>biggestCount.size()){
- biggestCount=count;
- }
- }
- // return the biggestCount.
- return biggestCount;
- }
|