Advertisement
axyd

LongestSequenceOfConsecutiveZeroes

May 28th, 2020
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. string longestZero(string);
  6.  
  7. int main(){
  8.     string sequence;
  9.  
  10.     do{
  11.         cout<< "Enter binary sequence (q/Q to quit)";
  12.         getline(cin,sequence);
  13.  
  14.         if(sequence == "q" || sequence == "Q") break;   //user wants to quit
  15.  
  16.         cout<< "\tLongest sequence: "<< longestZero(sequence)<< endl;
  17.  
  18.     } while (sequence != "q" && sequence != "Q");
  19.  
  20.     return 0;
  21. }
  22.  
  23. string longestZero(string seq){
  24.     string longest= "";
  25.     int rep= 0, i= 0, highest;
  26.  
  27.     while(i<seq.size()){
  28.         if(seq.at(i) == '0'){   //count zeros
  29.             if(++rep>highest) highest= rep;
  30.         }
  31.  
  32.         if(seq.at(i) == '1') rep= 0;    //reset counter
  33.  
  34.         ++i;
  35.     }
  36.     longest.insert(0, highest, '0');
  37.     return longest;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement