Given A String S Find The Number Of White Spaces In The Given String S.
Example:
Input: s = "Hello World"
Output: 1
Approach
C++
#include <bits/stdc++.h>using namespace std;int countWhiteSpaces(string s){int n = s.size(), i = 0;int cnt = 0;while (i < n){while (i < n && (s[i] == ' ')){cnt++;i++;}i++;}return cnt;}int main(){string s = "Hello World";cout << countWhiteSpaces(s) << "\n";return 0;}
No comments:
Post a Comment