aboutsummaryrefslogtreecommitdiff
path: root/challenge-308/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-308/ulrich-rieke/cpp/ch-2.cpp')
-rwxr-xr-xchallenge-308/ulrich-rieke/cpp/ch-2.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-308/ulrich-rieke/cpp/ch-2.cpp b/challenge-308/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..8a80cf6fc5
--- /dev/null
+++ b/challenge-308/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <vector>
+
+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 numbers separated by whitespace!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> encoded ;
+ for ( auto s : tokens )
+ encoded.push_back( std::stoi( s ) ) ;
+ std::cout << "Enter an initial integer!\n" ;
+ int initial ;
+ std::cin >> initial ;
+ std::vector<int> decoded ;
+ decoded.push_back( initial ) ;
+ //to find x in a xor x = b you can do a xor b since xor is its own
+ //inverse function!
+ for ( auto it = encoded.begin( ) ; it != encoded.end( ) ; ++it ) {
+ int last = decoded[decoded.size( ) - 1] ;
+ decoded.push_back( last ^ *it ) ;
+ }
+ std::cout << "( " ;
+ for ( int i : decoded ) {
+ std::cout << i << ' ' ;
+ }
+ std::cout << ")\n" ;
+ return 0 ;
+}
+