From 5046ee0948a6df543527521035cf488130cbd777 Mon Sep 17 00:00:00 2001 From: Arpna <100352419+Arpcoder@users.noreply.github.com> Date: Tue, 14 May 2024 00:20:55 +0530 Subject: [PATCH 1/4] Added Word counter project --- contrib/mini-projects/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/mini-projects/index.md b/contrib/mini-projects/index.md index 82596a2f..74bbb627 100644 --- a/contrib/mini-projects/index.md +++ b/contrib/mini-projects/index.md @@ -1,3 +1,3 @@ # List of sections -- [Section title](filename.md) +- [Word Counter](wordcounter.md) From 3819d508699a9cdcc36aa41525e143e78d06cc81 Mon Sep 17 00:00:00 2001 From: Arpna <100352419+Arpcoder@users.noreply.github.com> Date: Tue, 14 May 2024 00:43:36 +0530 Subject: [PATCH 2/4] Create wordcounter.md --- contrib/mini-projects/wordcounter.md | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 contrib/mini-projects/wordcounter.md diff --git a/contrib/mini-projects/wordcounter.md b/contrib/mini-projects/wordcounter.md new file mode 100644 index 00000000..0dce3998 --- /dev/null +++ b/contrib/mini-projects/wordcounter.md @@ -0,0 +1,45 @@ +# Word Counter +This project counts the number of words entered in the input sentence and give the number (count) of words in output. +Each element present in the text will be considered a letter, and a group of letters with a space will be considered a word. +It takes string as input from user, and counts the total number of words in the string. This count is displayed as output.
+Sample Input:- Harry likes Open Source
+Sample Output:- 4
+Explaination:- The above sentence contain 4 words.
+ +# Components of the code: +The provided code defines a Python function count_words(sentence) aimed at counting the number of words in a given sentence. Here's a step-by-step description: + +- ### Function Definition: + + The code begins with the definition of a function named count_words. + It takes a single parameter sentence, representing the input string whose words are to be counted. +- ### Splitting the Sentence: + + Within the function, the input sentence is split into individual words using the split() method. + The split() method divides the string into substrings based on whitespace characters (spaces, tabs, and newline characters) by default. +- ### Counting Words: + + The code determines the number of words in the sentence by calculating the length of the list generated from splitting the sentence. + The len() function is utilized to compute the length of the list, which corresponds to the number of words in the sentence. +- ### Returning the Count: + + After determining the word count, the function returns this value. +- ### Example Usage: + + The script prompts the user to input a sentence using the input() function. + It then calls the count_words function, passing the user's input as an argument, and stores the returned word count in the variable number_of_words. + Finally, it prints out the number of words in the sentence. + + def count_words(sentence): + # Split the sentence into words + words = sentence.split() + + # Count the number of words + word_count = len(words) + + return word_count + + # Example usage: + sentence = input("Enter a sentence: ") + number_of_words = count_words(sentence) + print("Number of words:", number_of_words) From b17d61f5a062f0e19bcb164a333580b6ba521e95 Mon Sep 17 00:00:00 2001 From: Arpna <100352419+Arpcoder@users.noreply.github.com> Date: Fri, 17 May 2024 08:35:09 +0530 Subject: [PATCH 3/4] Corrected the formatting of code --- contrib/mini-projects/wordcounter.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/contrib/mini-projects/wordcounter.md b/contrib/mini-projects/wordcounter.md index 0dce3998..0c08e2eb 100644 --- a/contrib/mini-projects/wordcounter.md +++ b/contrib/mini-projects/wordcounter.md @@ -31,15 +31,16 @@ The provided code defines a Python function count_words(sentence) aimed at count Finally, it prints out the number of words in the sentence. def count_words(sentence): - # Split the sentence into words - words = sentence.split() + # Split the sentence into words + words = sentence.split() - # Count the number of words - word_count = len(words) - - return word_count + # Count the number of words + word_count = len(words) + return word_count + # Example usage: sentence = input("Enter a sentence: ") number_of_words = count_words(sentence) print("Number of words:", number_of_words) + From dc3e5b80be7ed187fd96a75f01c7e8b883006f0b Mon Sep 17 00:00:00 2001 From: Arpna <100352419+Arpcoder@users.noreply.github.com> Date: Fri, 17 May 2024 11:50:39 +0530 Subject: [PATCH 4/4] Updated indentation of the python code --- contrib/mini-projects/wordcounter.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contrib/mini-projects/wordcounter.md b/contrib/mini-projects/wordcounter.md index 0c08e2eb..5f865da5 100644 --- a/contrib/mini-projects/wordcounter.md +++ b/contrib/mini-projects/wordcounter.md @@ -30,7 +30,7 @@ The provided code defines a Python function count_words(sentence) aimed at count It then calls the count_words function, passing the user's input as an argument, and stores the returned word count in the variable number_of_words. Finally, it prints out the number of words in the sentence. - def count_words(sentence): + def count_words(sentence): # Split the sentence into words words = sentence.split() @@ -39,8 +39,8 @@ The provided code defines a Python function count_words(sentence) aimed at count return word_count - # Example usage: - sentence = input("Enter a sentence: ") - number_of_words = count_words(sentence) - print("Number of words:", number_of_words) + # Example usage: + sentence = input("Enter a sentence: ") + number_of_words = count_words(sentence) + print("Number of words:", number_of_words)