-
Notifications
You must be signed in to change notification settings - Fork 2
/
wordcount.cpp
41 lines (32 loc) · 962 Bytes
/
wordcount.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Word Count
#string manipulation
Have the function WordCount(str) take the str string parameter being passed and return
the number of words the string contains (e.g. "Never eat shredded wheat or cake"
would return 6). Words will be separated by single spaces.
Optimal: o(n), achieved: o(n)
*/
#include <iostream>
#include <string>
int WordCount(std::string str) {
// code goes here
size_t counter{};
size_t startInd{};
size_t endInd{str.length() - 1};
// 'remove'trailing and leading spaces
while (str.at(startInd) == ' ') { startInd++; }
while (str.at(endInd) == ' ') { endInd--; }
// start at startInd
size_t pos{startInd};
// until you reach the and of the string
do {
if (str.at(pos) == ' ') {counter++;}
pos = str.find(" ",pos+1);
} while (pos < endInd);
return ++counter;
}
int main(void) {
// keep this function call here
std::cout << WordCount(coderbyteInternalStdinFunction(stdin));
return 0;
}