Word counter

 It is very hard for a computer to actually count words in a given sentence. That's because words are made of different combinations of 26+ letters in the alphabet, so a clean definition of what a word is is hard to create. However, there's a roundabout way to find the number of words in a sentence, which is the count the spaces. Counting the spaces doesn't really count the words, but it has the same effect of counting the words, and I think that's what programming is about. It's like measuring 4 litres of water when you're computer can only calculate 5 litres and 3 litres of water. There's no 4 litre bottle, so you have to find a roundabout way to get 4 litres of water, using the available tools that are given to you.

Of course if someone wanted to purposely mess with the working of the program, by inserting random spaces, it will not work properly.

sentence = input("Please enter your sentence: ")
counter = 1
for x in range(0, len(sentence)):
if sentence[x] == " ":
counter += 1
print("There are " + str(counter) + " words in that sentence.")

Comments