From c34bb5d7bd7fce08e8311a0f527ce7fbd69e4dae Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 26 Jan 2021 17:36:17 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-097/ulrich-rieke/cpp/ch-1.cpp | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-097/ulrich-rieke/cpp/ch-1.cpp (limited to 'challenge-097/ulrich-rieke/cpp/ch-1.cpp') diff --git a/challenge-097/ulrich-rieke/cpp/ch-1.cpp b/challenge-097/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..d69135b4cc --- /dev/null +++ b/challenge-097/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +char findCipher( char c, int shift ) { + static const std::string alphabet {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"} ; + std::string mapped { alphabet.substr(26 - shift) } ; + std::reverse ( mapped.begin( ) , mapped.end( ) ) ; + mapped.append( alphabet.substr(0 , 26 - shift ) ) ; + if ( c == ' ' ) + return ' ' ; + else { + auto found = std::find( alphabet.begin( ) , alphabet.end( ) , c ); + return mapped[ static_cast(std::distance(alphabet.begin( ), found))] ; + } +} + +int main( int i , char* argv[ ] ) { + if ( i != 3 ) { + std::cerr << "usage: challenge097 !\n" ; + return 1 ; + } + int leftshift = std::atoi( argv[ 2 ] ) ; + int realshift = leftshift % 26 ; + std::string plaintext( argv[ 1 ] ) ; + int len = plaintext.length( ) ; + std::string ciphertext ; + for ( char letter : plaintext ) { + ciphertext.push_back( findCipher ( letter , realshift ) ) ; + } + std::cout << ciphertext << std::endl ; + return 0 ; +} -- cgit