Count words in a sentence

Problem Statement

Consider that a student is writing an assignment, which has to be of at least some 100 words. And we would like to help him with the word count.

For a part of that solution, write a Python function that takes a sentence as argument, and return the number of words in that sentence.

Input

The first line of input consists of the sentence, a string.

Output

Print the number of words in the given sentence.

As is

The boilerplate code is already there to read the input from user, in the main() function, and to print the value returned by the countWords() function. Do not edit this code.

To do ✍

countWords() has one parameter: sentence. Complete the function that counts the words in given sentence.

Assume that the words are separated by a single whitespace character.

Example 1

Test input

This is a sample paragraph.

Test output

5

Explanation

There are five words in the sentence. Hence the output 5.

Example 2

Test input

Apple is red. Banana is yellow. Cherry is red.

Test output

9

Explanation

There are 9 words in the given sentence.

Useful References

Python Program

# Complete the following function
def countWords(sentence):




# Boilerplate code - Do not edit the following
def main():
    sentence = input()
    print(countWords(sentence))

if __name__ == "__main__":
    main()

Input and Outputs 1

a ba c d e f g h i j k lm n o p q.
16

Input and Outputs 2

Apple is red
3

Input and Outputs 3

This is a sample paragraph.
5
# Complete the following function
def countWords(sentence):
    count = len(sentence.split())
    return count

# Boilerplate code - Do not edit the following
def main():
    sentence = input()
    print(countWords(sentence))

if __name__ == "__main__":
    main()
show answer
main.py
⏵︎ Run Tests
Reset
Download
×
Code copied to clipboard successfully 👍