From 3114956979e812e8ca166cb84d8dea346cee22d3 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Mon, 30 Sep 2024 22:07:13 +0100 Subject: - 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. --- challenge-289/ulrich-rieke/cpp/ch-2.cpp | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 challenge-289/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-289/ulrich-rieke/cpp/ch-2.cpp') 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 +#include +#include +#include +#include +#include + +std::vector split( std::string & text , char delimiter ) { + std::vector 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, to end!\n" ; + std::vector block ; + std::string line ; + std::getline( std::cin , line ) ; + while ( ! line.empty( ) ) { + block.push_back( line ) ; + std::getline( std::cin , line ) ; + } + std::vector shuffled ; + for ( auto aLine : block ) { + std::vector 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 ; +} + + + -- cgit