aboutsummaryrefslogtreecommitdiff
path: root/challenge-033/ulrich-rieke/cpp/ch-1.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-033/ulrich-rieke/cpp/ch-1.cpp')
-rw-r--r--challenge-033/ulrich-rieke/cpp/ch-1.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-033/ulrich-rieke/cpp/ch-1.cpp b/challenge-033/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..563d91befa
--- /dev/null
+++ b/challenge-033/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <map>
+#include <cctype>
+#include <fstream>
+#include <algorithm>
+#include <string>
+
+char myTransformer( char d ) {
+ if ( std::isupper( d ) )
+ return std::tolower( d ) ;
+ else
+ return d ;
+}
+
+int main( int argc, char* argv[] ) {
+ for ( int i = 1 ; i < argc ; i++ ) {
+ std::string file( argv[ i ] ) ;
+ std::ifstream infile( file , std::ios::binary | std::ios::in ) ;
+ if ( infile ) {
+ std::map<char, int> frequencies ;
+ while ( infile.good( ) ) {
+ std::string line ;
+ std::getline( infile, line ) ;
+ std::transform( line.begin( ) , line.end( ) , line.begin( ) ,
+ myTransformer ) ;
+ for ( char c : line ) {
+ if ( std::isalpha( c ) ) {
+ frequencies[c]++ ;
+ }
+ }
+ }
+ infile.close( ) ;
+ std::cout << "Letter frequency for file " << file << " :\n" ;
+ for ( auto & p : frequencies ) {
+ std::cout << p.first << ": " << p.second << std::endl ;
+ }
+ }
+ else {
+ std::cout << "Can't open file " << file << " !\n" ;
+ }
+ }
+}