fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // return true if all characters ('0' or '1') in the range [x,y] are the same
  5. bool is_same( const std::string& str, unsigned long x, unsigned long y )
  6. {
  7. if( x > y || str.size() < (y-1) ) return false ;
  8. return ( str[x] == '0' || str[x] == '1' ) &&
  9. str.find_first_not_of( str[x], x ) > y ;
  10. }
  11.  
  12. int main ()
  13. {
  14. std::string str ;
  15. std::cout << "enter a string and press RETURN\n" ;
  16. std::getline( std::cin, str ) ; // 10110111111111111000111000001110001110001
  17. std::cout << "string: " << str << '\n' ;
  18. std::size_t x = 5 ;
  19. std::size_t y = 15 ;
  20. if( is_same( str, x, y ) ) std::cout << "Yes\n" ;
  21. }
  22.  
Success #stdin #stdout 0s 3032KB
stdin
10110111111111111000111000001110001110001
stdout
enter a string and press RETURN
string: 10110111111111111000111000001110001110001
Yes