
									 Problem
								
								
									 Solution
								
								
									 Submissions
								
								
							Number of Words in a String
								Certification: Basic Level
								Accuracy: 50%
								Submissions: 12
								Points: 5
							
							Write a C++ program that counts the number of words in a given string. A word is defined as a sequence of non-space characters.
Example 1
- Input: string = "Hello World"
 - Output: 2
 - Explanation: 
- Step 1: Split the input string by spaces.
 - Step 2: Count the number of non-empty elements.
 - Step 3: Return the count.
 
 
Example 2
- Input: string = " This is a test "
 - Output: 4
 - Explanation: 
- Step 1: Split the input string by spaces.
 - Step 2: Count the number of non-empty elements after removing leading and trailing spaces.
 - Step 3: Return the count.
 
 
Constraints
- 1 ≤ len(string) ≤ 10^6
 - String contains printable ASCII characters
 - Time Complexity: O(n), where n is the length of the string
 - Space Complexity: O(1)
 
Editorial
									
												
My Submissions
										All Solutions
									| Lang | Status | Date | Code | 
|---|---|---|---|
| You do not have any submissions for this problem. | |||
| User | Lang | Status | Date | Code | 
|---|---|---|---|---|
| No submissions found. | ||||
Solution Hints
- Iterate through the string and count the number of transitions from space to non-space characters.
 - Handle leading and trailing spaces separately.
 - Consider edge cases where the string is empty or contains only spaces.