diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-30 22:07:13 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-30 22:07:13 +0100 |
| commit | 3114956979e812e8ca166cb84d8dea346cee22d3 (patch) | |
| tree | c15e952faad2616c26483396368178c686903f6e /challenge-289/ulrich-rieke/cpp/ch-2.cpp | |
| parent | 8c1a0beec1d0dac09dd376bedb36eb11cc56dfbb (diff) | |
| download | perlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.tar.gz perlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.tar.bz2 perlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.zip | |
- Added solutions by Ulrich Rieke.
- Added solutions by Paulo Custodio.
- Added solutions by David Ferrone.
- Added solutions by Thomas Kohler.
- Added solutions by Torgny Lyon.
Diffstat (limited to 'challenge-289/ulrich-rieke/cpp/ch-2.cpp')
| -rwxr-xr-x | challenge-289/ulrich-rieke/cpp/ch-2.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/challenge-289/ulrich-rieke/cpp/ch-2.cpp b/challenge-289/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..b03c0abd30 --- /dev/null +++ b/challenge-289/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,64 @@ +#include <vector>
+#include <string>
+#include <random>
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+
+std::vector<std::string> split( 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 ;
+}
+
+std::string do_shuffle( const std::string & word ) {
+ std::random_device rd ;
+ std::mt19937 g ( rd( ) ) ;
+ std::string central { word.substr( 1 , word.length( ) - 2 ) } ;
+ std::string output { word.substr( 0 , 1 ) } ;
+ std::shuffle( central.begin( ) , central.end( ) , g ) ;
+ output = output + central ;
+ output.push_back( word.back( ) ) ;
+ return output ;
+}
+
+int main( ) {
+ std::cout << "Enter some sentences, <return> to end!\n" ;
+ std::vector<std::string> block ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ while ( ! line.empty( ) ) {
+ block.push_back( line ) ;
+ std::getline( std::cin , line ) ;
+ }
+ std::vector<std::string> shuffled ;
+ for ( auto aLine : block ) {
+ std::vector<std::string> shuffled_line ;
+ auto tokens { split( aLine , ' ' ) } ;
+ for ( auto w : tokens ) {
+ if ( w.length( ) <= 3 ) {
+ shuffled_line.push_back( w ) ;
+ }
+ else {
+ shuffled_line.push_back( do_shuffle( w ) ) ;
+ }
+ }
+ std::string changed ;
+ for ( auto w : shuffled_line ) {
+ changed = changed + w + " " ;
+ }
+ changed.pop_back( ) ;
+ shuffled.push_back( changed ) ;
+ }
+ for ( auto w : shuffled ) {
+ std::cout << w << '\n' ;
+ }
+ return 0 ;
+}
+
+
+
|
