aboutsummaryrefslogtreecommitdiff
path: root/challenge-280/ulrich-rieke/cpp/ch-2.cpp
blob: d228e4ce60f4159b27c5a46b64f014b603ef05be (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
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <algorithm>

int main( ) {
   std::cout << "Enter a string with some vertical bars!\n" ;
   std::string line ;
   std::cin >> line ;
   if ( std::count( line.begin( ) , line.end( ) , '|' ) <= 1 ) {
      std::cout << std::count( line.begin( ) , line.end( ) , '*' ) << 
	 '\n' ;
   }
   else {
      bool barSeen = false ;
      std::string filtered ;
      for ( int i = 0 ; i < line.length( ) ; i++ ) {
	 char c = line[i] ;
	 if ( c != '|' ) 
	    filtered.append( 1, c ) ;
	 else {
	    if ( barSeen ) {
	       auto pos = filtered.find( '|' ) ;
	       filtered = filtered.substr( 0 , pos ) ;
	       barSeen = false ;
	    }
	    else {
	       filtered.append( 1 , '|' ) ;
	       barSeen = true ;
	    }
	 }
      }
      std::cout << std::count( filtered.begin( ) , filtered.end( ) , 
	    '*' ) << '\n' ;
   }
   return 0 ;
}