Program to count the total number of words in a string

Explanation

In this program, we need to count the words present in the string.

Total number of words present in the string is 7.

Algorithm

  1. Define a string.
  2. To counts the words present in the string, we will iterate through the string and count the spaces present in the string. As each word always ends with a space.
  3. If a string starts with a space, then we must not count the first space as it is not preceded by a word.
  4. To count the last word, we will increment the count by 1.

Solution

Python

Output:

Total number of words in the given string: 7

C

Output:

Total number of words in the given string: 7

JAVA

Output:

Total number of words in the given string: 7 

C#

Output:

Total number of words in the given string: 7

PHP

Output:

Total number of words in the given string: 7

Next Topic#




Latest Courses