Skip to content

Added Words Counter #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions W/words-counter/Readme.md
Original file line number Diff line number Diff line change
@@ -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.<br>
Inside the function, the split method is used to break the text into words by splitting it at whitespace (spaces, tabs, and line breaks).<br>
The len function is used to count the number of words in the list. <br>
The program then takes user input using input and calls the word_count function with the provided input. <br>
Finally, it prints the word count to the console.
9 changes: 9 additions & 0 deletions W/words-counter/words_counter.py
Original file line number Diff line number Diff line change
@@ -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}")