From aaab417272f7ae13ade34c68a033d2b1214886d3 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Mon, 3 Feb 2025 17:29:40 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-307/ulrich-rieke/cpp/ch-2.cpp | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 challenge-307/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-307/ulrich-rieke/cpp/ch-2.cpp') diff --git a/challenge-307/ulrich-rieke/cpp/ch-2.cpp b/challenge-307/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..8d8a8dac18 --- /dev/null +++ b/challenge-307/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +std::vector split( const 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 ; +} + +bool areAnagrams( std::string firstWord , std::string secondWord ) { + std::sort( firstWord.begin( ) , firstWord.end( ) ) ; + std::sort( secondWord.begin( ) , secondWord.end( ) ) ; + return (firstWord == secondWord ) ; +} + +int count_anagrams( const std::vector & words ) { + int len = words.size( ) ; + int anagramcount = 0 ; + for ( int i = 0 ; i < len - 1 ; i++ ) { + if ( areAnagrams( words[i] , words[i + 1] ) ) + anagramcount++ ; + } + return anagramcount ; +} + +int main( ) { + std::cout << "Enter some words separated by whitespace!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + auto tokens { split( line , ' ' ) } ; + std::cout << ( tokens.size( ) - count_anagrams( tokens ) ) << '\n' ; + return 0 ; +} -- cgit