85
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
- Define a string.
- 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.
- If a string starts with a space, then we must not count the first space as it is not preceded by a word.
- 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#