From 44d085f265583f0e3cbef294bbe2c8e300aaa452 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 3 Apr 2024 17:47:07 +0300 Subject: [w6] Added exercise descriptions and solutions to 1-9 --- week06/Exercise05.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 week06/Exercise05.cpp (limited to 'week06/Exercise05.cpp') diff --git a/week06/Exercise05.cpp b/week06/Exercise05.cpp new file mode 100644 index 0000000..f677043 --- /dev/null +++ b/week06/Exercise05.cpp @@ -0,0 +1,31 @@ +#include +#include + +int main() { + char fileName[1024]; + std::cin.getline(fileName, 1024); + + std::ifstream file(fileName); + if (!file.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + return 1; + } + + unsigned lines = 0; + unsigned words = 0; + char prev = '\0'; + + while (!file.eof()) { + if (file.peek() == '\n') { + lines++; + } + if ((prev == ' ' || prev == '\t' || prev == '\n') && file.peek() != ' ' && file.peek() != '\t' && file.peek() != '\n') { + words++; + } + + prev = file.get(); + } + file.close(); + + std::cout << lines << " " << words << std::endl; +} -- cgit v1.2.3