Count the number of words in the string. A word is one or more non-blank characters separated by one or more blanks. My suggestion is to use a flag (a boolean variable) to indicate whether or not you are in a word. Then you know you have found a word when the flag indicates that you are in a word and the next character is a blank. Think about how you will know when you have found the end of a word, and what you should do with your flag at that point.

Respuesta :

ijeggs

Answer:

myString = input("Please enter a string")

wordList = myString.split()

numofWords = len(wordList)

print('The number of words in {} is {}'.format(myString, numofWords))

Explanation:

Since the suggestion to use a boolean variable flag is not compulsory, I have implemented it in another way.

Using python programming language, the input function is used to prompt the user to enter a string which is saved in a variable myString.

The string entered is converted into a list of all the words by spliting at the blank spaces and saved in the variable wordList

Next we count the number of words by obtaining the len of the list and output to the user

ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE