Posts

Showing posts with the label count the number of words in a string

Python program to count the number of words in a string

Image
Count the number of words in a string Problem description : The program gets a string from the user and returns the number of words in the string. Example, string, s = "Hello everyone, welcome to techieebytes! Here we are going to look at a python program to count the number of words in a string" The number of words = 24 Method 1 : In this method, we are using a for loop to traverse through the entire string character by character and increment the count variable if the character is either a space (" ") or full stop ("."). Procedure : 1. Start. 2. Get the string as input from the user. 3. initialize count as 0. 4. Iterate through each character in a string.     => check if the character is equal to space (" ") or full stop (".").      => increment the count variable. 5. Print the value of count. 6. End. Code : #get the string from the user s = str(input("Enter the string : ")) #initialize count variable count=0 for i in