####### STRINGS PRACTICE ##### Count Words
- Get link
- X
- Other Apps
Problem Statement
Suggest Edit
For a given input string(str), find and return the total number of words present in it.
It is assumed that two words will have only a single space in between. Also, there wouldn't be any leading and trailing spaces in the given input string.
Input Format:
The first and only line of input contains a string without any leading and trailing spaces.
Output Format:
The only line of output prints an integer value denoting the total number of words present in the string.
Note:
You are not required to print anything. It has already been taken care of.
Constraints:
0 <= N <= 10^3
Where N is the length of the input string.
Time Limit: 1 sec
Sample Input 1:
Coding Ninjas!
Sample Output 1:
2
Sample Input 2:
this is a sample string
Sample Output 2:
5
from sys import stdin def countWords(string) : # Your code goes here return len(string.split()) #main string = stdin.readline().strip(); count = countWords(string) print(count)
- Get link
- X
- Other Apps
Comments
Post a Comment