aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/atschneid/cpp/ch-2.cpp
blob: 808c66067b078296b85e6c6b6eccfcc73fe51e35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int validate_a_b_string_v1( std::string s ) {
  // this is the boring algorithm that walks through each char in s
  bool first_b = false;
  for ( char sc : s ) {
    // update if we've seen a 'b' yet
    first_b |= ( sc == 'b' );
    if (first_b && sc == 'a') {
      // if we see an 'a' after a 'b' then false
      return false;
    }
  }
  // if we haven't seen any 'b's then first_b is still false
  return first_b;
}

int main(){
  std::string inputs[] = { "aabb", "abab", "aaa", "bbb", "aaaffeoioijlkfjoihslkjhfb" };
  
  for (auto s : inputs ) {
    std::cout << s << " :: "
	      << ( validate_a_b_string_v1( s )  ? "true" : "false" )
	      << "\n";
  }
}