diff options
Diffstat (limited to 'challenge-280/ulrich-rieke/cpp/ch-2.cpp')
| -rwxr-xr-x | challenge-280/ulrich-rieke/cpp/ch-2.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-280/ulrich-rieke/cpp/ch-2.cpp b/challenge-280/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..d228e4ce60 --- /dev/null +++ b/challenge-280/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,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 ;
+}
|
