aboutsummaryrefslogtreecommitdiff
path: root/challenge-307/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-02-03 17:29:40 +0000
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-02-03 17:29:40 +0000
commitaaab417272f7ae13ade34c68a033d2b1214886d3 (patch)
treefaa6dbd23c6f83d75adb7d17ae31402afcb1dc8e /challenge-307/ulrich-rieke/cpp/ch-2.cpp
parent22771160a2fd5e5893342b5f28ee039872842852 (diff)
downloadperlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.tar.gz
perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.tar.bz2
perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-307/ulrich-rieke/cpp/ch-2.cpp')
-rwxr-xr-xchallenge-307/ulrich-rieke/cpp/ch-2.cpp40
1 files changed, 40 insertions, 0 deletions
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 <string>
+#include <iostream>
+#include <vector>
+#include <sstream>
+#include <algorithm>
+
+std::vector<std::string> split( const 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 ;
+}
+
+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<std::string> & 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 ;
+}