From 589a5caf6f59c103d5dc5345a01146c5526de726 Mon Sep 17 00:00:00 2001 From: Abdul Rehman <105493274+AbdulRehmanGHub@users.noreply.github.com> Date: Sun, 15 Oct 2023 06:04:55 +0000 Subject: [PATCH] Added Words Counter --- W/words-counter/Readme.md | 9 +++++++++ W/words-counter/words_counter.py | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 W/words-counter/Readme.md create mode 100644 W/words-counter/words_counter.py diff --git a/W/words-counter/Readme.md b/W/words-counter/Readme.md new file mode 100644 index 00000000..1abdfaf5 --- /dev/null +++ b/W/words-counter/Readme.md @@ -0,0 +1,9 @@ +## Words Counter in Python +This is a very simple program and we can easily use it and count our words + +## Note +The word_count function takes a string text as input.
+Inside the function, the split method is used to break the text into words by splitting it at whitespace (spaces, tabs, and line breaks).
+The len function is used to count the number of words in the list.
+The program then takes user input using input and calls the word_count function with the provided input.
+Finally, it prints the word count to the console. diff --git a/W/words-counter/words_counter.py b/W/words-counter/words_counter.py new file mode 100644 index 00000000..9919ea1a --- /dev/null +++ b/W/words-counter/words_counter.py @@ -0,0 +1,9 @@ +def word_count(text): + # Split the text into words using whitespace as the separator + words = text.split() + return len(words) + +if __name__ == "__main__": + input_text = input("Enter a text: ") + count = word_count(input_text) + print(f"Word count: {count}")