Number of Words in a String in Python

In this class, we write a program to find the number of words in a string in python.

For Complete YouTube Video: Click Here

Number of Words in a String

We can quickly write code in a single line in python using the split method.

We intend to improve the coding skill of the reader. We find this example is good to improve coding skill.

Write the code using loops and if conditions. Do not use the python methods.

Logic

Build the logic by taking different examples.

Example 1:

x=”hello how are you.”

In the example string given above, hello space how space is provided.

By looking at we can understand read the string character by character.

Whenever we find space, increment the word count by one.

But this logic does not work. Check the second situation.

Example 2:

x="hello   how are you"

In the second example, there are three spaces between hello and how.

The logic we identified above will find the first space and increment word count.

Immediately it will find a second space and increment word count. Which is not correct. So change the logic.

The new logic we get to our mind is. Whenever space increment word counts and moves to next word.

But the second logic also not word in some examples. These examples are given in example 3.

Example 3:

x="   hello    how are you"

The third example has space in the beginning.

The logic identified from the second example does not work here. why?

Whenever space increment word counts and moves to next word.

If we find space in the beginning, then do not increment word count.

Remaining situations, we can use the logic identified from the second example.

Do not increment word count. if space is found at the starting.

Example 4:

x="   hello     how are you"

The logic identified from the above three examples will not identify the last word.

For the last word, in example four.

After the last word, space is not present.

So the word is not counted in the word count.

From this example, add a new logic.

Coming out of the loop after completing all the characters. Increment word count by one.

This extra condition mentioned in example four also not working in some instances.

Example 5:

x=" hello how are you     "

In example five, after the last word, there are spaces.

We change the condition with this new example.

Whenever space increment word counts and moves to next word or end of the string.

All these five different situations keep in mind and build the logic.

The below program is implemented by considering all the situations provided in these five examples.

For better coding practice, analyze the code given below on your own.

Program

string=input("enter the string")
words=0
ch=0
length=len(string)
print(length)
while(ch<length):
    if(ch==0 and string[ch]==" "):# condition for empty space at the begining
        while(ch<length):
            if(string[ch]==" "):
                ch+=1
            else:
                break
    elif(string[ch]!=" "):# condition to move on words
        if(ch+1 ==length):# condition for last word with out space
            ch+=1
            words+=1
        else:
            ch+=1
    else:    # condition to increase word count when space is identified
        words+=1
        while(string[ch]==" " and ch<length):
            if(ch+1<length):
                ch+=1
            else:  # condition to break end of string when spaces
                ch+=1
                break
print(words)