From 2952047fe741ca5b3717e066e0a6aeac699ee4c1 Mon Sep 17 00:00:00 2001 From: Oskar Date: Fri, 30 Aug 2024 15:14:27 +0200 Subject: more --- 5p14.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 5p14.cpp (limited to '5p14.cpp') diff --git a/5p14.cpp b/5p14.cpp new file mode 100644 index 0000000..5c032e1 --- /dev/null +++ b/5p14.cpp @@ -0,0 +1,45 @@ +#include + +/* + * + * 5.14 + * + * The program does not handle cases where words repeat for + * the same amount of times. So for example if we input + * "hello hello hi hi" only hello would print as the top word + */ + +int main () { + + std::string NewWord; + std::string TempWord; + std::string MostOccuring; + uint32_t TmpCount = 0; + uint32_t TopCount = 0; + while(std::cin >> NewWord) { + if(TempWord != NewWord) { + if(TmpCount > TopCount) { + TopCount = TmpCount; + MostOccuring = TempWord; + } + + TempWord = NewWord; + TmpCount = 1; + } else if(TempWord == NewWord) { + ++TmpCount; + if(TmpCount > TopCount) { + TopCount = TmpCount; + MostOccuring = TempWord; + } + } + } + + + if(TopCount > 1) { + std::cout << MostOccuring << " is the top word occuring " << TopCount << " times." << std::endl; + } else { + std::cout << "No words were repeated!" << std::endl; + } + + return 0; +} -- cgit v1.2.3