Python program to count the number of words in a string

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 s:

        #check if the character is equal to space or full stop.

if(i==" " or i=="."):

                #increment the count variable

count+=1

#print count

print("Number of words = ", count)

Sample Input :

s = "Hi all, welcome to techieebytes."

Sample Output :

Number of words = 5

Output screenshot :

Count the number of words in a string
Word count - Output 1



Method 2 :

In this method, we are converting the string into a list of words by splitting it on the basis of the space using split() function and then returning the length of the list. The length of the list is equal to the number of words.

Procedure :

1. Start.

2. Get the string as input from the user.

3. convert the string into list using split function.

4. Print the length of the list.

5. End.

Code :

#get the string from user

s = str(input("Enter the string : "))

#convert the string to list using split

l=s.split(" ")

#print the length of the list

print("Number of words = ",len(l))

Sample Input :

s = "My first python program."

Sample Output :

Number of words = 4

Output screenshot :

Count the number of words in a string
Word count - Output 2



Method 3 :

In this method, we are going to count the number of white spaces (" ") in the string using the builtin count() function. The number of words will be equal to 1 greater than the count of white spaces.

Procedure :

1. start.

2. Get  the string as input from the user.

3. Count the number of white spaces in the string using count() function.

4. Print the number of words by adding 1 to the space count.

5. End.

code :

#Get the string from the user

s = str(input("Enter the string : "))

#Count the number of whitespaces

space_count = s.count(" ")

#print white space count +1

print("Number of words = ", space_count+1)

Sample input :

s = " Python has major roles in machine learning and other technologies"

Sample output :

Number of words = 10

Output screenshot :

Count the number of words in a string
word count - output 3


Comments

Popular posts from this blog

Finding the percentage in python - HackerRank solution

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

ginortS in Python - HackerRank solution