count words in string

text = input("Enter a string: ").strip()  # Remove leading/trailing spaces

count = 0
print(len(text))
for i in range(len(text)):
    if text[i] == " " and text[i - 1] != " ":  # Count spaces between words
        count += 1
print(count)
# If the string is not empty, add 1 to count the last word
if text:
    count += 1

print("Word count:", count)

Leave a Reply

Your email address will not be published. Required fields are marked *