From 56ffaee884c72324292ec6d3e69b4ee6708dbeae Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 1 Jul 2020 21:42:13 +0100 Subject: - Added C++ solution by Ulrich Rieke. --- challenge-067/ulrich-rieke/cpp/ch-2.cpp | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 challenge-067/ulrich-rieke/cpp/ch-2.cpp diff --git a/challenge-067/ulrich-rieke/cpp/ch-2.cpp b/challenge-067/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..d31d4e8c33 --- /dev/null +++ b/challenge-067/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +std::vector combineStrings( const std::string & worda , + const std::string & wordb) { + std::vector combis ; + int lena = worda.size( ) ; + int lenb = wordb.size( ) ; + if ( lena > 0 && lenb == 0 ) { + for ( int i = 0 ; i < lena ; i++ ) { + combis.push_back( worda.substr( i , 1 ) ) ; + } + } + if ( lena == 0 && lenb > 0 ) { + for ( int i = 0 ; i < lenb ; i++ ) { + combis.push_back( wordb.substr( i , 1 ) ) ; + } + } + if ( lena > 0 && lenb > 0 ) { + for ( int i = 0 ; i < lena ; i++ ) { + for ( int j = 0 ; j < lenb ; j++ ) { + combis.push_back( worda.substr( i , 1 ) + wordb.substr( j , 1 ) ) ; + } + } + } + return combis ; +} + +std::vector combineStringsWithString( std::vector + & words, std::string & expression ) { + std::vector newWords ; + if ( ! expression.empty( ) ) { + int len = expression.size( ) ; + for ( auto word : words ) { + for ( int i = 0 ; i < len ; i++ ) { + word.append( expression.substr( i , 1 )) ; + newWords.push_back( word ) ; + } + } + } + else + newWords = words ; + return newWords ; +} + +int main( int argc, char * argv[] ) { + std::map phone { {"1" , "_,@"} , {"2", "ABC" }, + {"3" , "DEF"} , {"4" , "GHI"} , {"5" , "JKL"} , {"6" , "MNO"} , + {"7" , "PQRS"} , {"8", "TUV"} , {"9", "WXYZ"} , {"0" , ""}} ; + std::string input( argv[1] ) ; + std::vector combis ; + int len = input.size( ) ; + if ( len == 1 ) { + std::string word ( phone[ input ] ) ; + if ( !word.empty( ) ) { + for ( int i = 0 ; i < word.length( ) ; i++ ) { + combis.push_back( word.substr( i , 1 ) ) ; + } + } + } + if ( len == 2 ) { + combis = combineStrings( phone[ input.substr( 0 , 1 )] , + phone[ input.substr( 1 , 1 )] ) ; + } + if ( len > 2 ) { + combis = combineStrings( phone[ input.substr( 0 , 1 )] , + phone[ input.substr( 1 , 1 )] ) ; + for ( int i = 2 ; i < len ; i++ ) { + combis = combineStringsWithString( combis, + phone[ input.substr( i , 1 )] ) ; + } + } + for ( auto & str : combis ) { + std::cout << str << " " ; + } + std::cout << std::endl ; + return 0 ; +} -- cgit