code.cpp 737 B

12345678910111213141516171819202122232425262728
  1. /* Write a function that returns the longest sequence
  2. * of consecutive zeroes in a binary string.
  3. */
  4. std::string longestZero(std::string str) {
  5. std::string count = "";
  6. std::string biggestCount = count;
  7. // 011000010110001
  8. // loop through each character in string
  9. for(int i=0;i<str.size();i++){
  10. // save each zero in the string to the end of count
  11. if(str[i] == '0'){
  12. count.append("0");
  13. }
  14. else if(str[i]=='1'){
  15. count = "";
  16. }
  17. // if the string is not a one or zero, this is not a binary string. program ABORTED!
  18. else {
  19. assert(false);
  20. }
  21. // compare each count to find the biggestCount
  22. if(count.size()>biggestCount.size()){
  23. biggestCount=count;
  24. }
  25. }
  26. // return the biggestCount.
  27. return biggestCount;
  28. }