diff options
Diffstat (limited to 'challenge-308/ulrich-rieke/cpp/ch-1.cpp')
| -rwxr-xr-x | challenge-308/ulrich-rieke/cpp/ch-1.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-308/ulrich-rieke/cpp/ch-1.cpp b/challenge-308/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..409cea5b7c --- /dev/null +++ b/challenge-308/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,39 @@ +#include <iostream>
+#include <string>
+#include <sstream>
+#include <vector>
+#include <map>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::istringstream istr { text } ;
+ std::string word ;
+ while ( std::getline( istr , word , delimiter ) ) {
+ tokens.push_back( word ) ;
+ }
+ return tokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some words separated by whitespace!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto firstTokens { split( line , ' ' ) } ;
+ std::string secondline ;
+ std::cout << "Enter some more words separated by whitespace!\n" ;
+ std::getline( std::cin , secondline ) ;
+ auto secondTokens { split( secondline , ' ' ) } ;
+ std::map<std::string , int> firstFrequency , secondFrequency ;
+ int common = 0 ;
+ for ( auto s : firstTokens )
+ firstFrequency[s]++ ;
+ for ( auto s : secondTokens )
+ secondFrequency[s]++ ;
+ for ( auto aPair : firstFrequency ) {
+ if ( secondFrequency.find( aPair.first ) != secondFrequency.end( ) ) {
+ common++ ;
+ }
+ }
+ std::cout << common << '\n' ;
+ return 0 ;
+}
|
