-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcount.cpp
More file actions
106 lines (95 loc) · 2.38 KB
/
wordcount.cpp
File metadata and controls
106 lines (95 loc) · 2.38 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
/* TODO: finish writing this example from class. Ideally, try to
* write it from scratch using the ideas from lecture. Also, try
* to extend it to account for newlines (in addition to space and
* tab characters for white space). */
size_t wordcount(const string& s)
{
bool space = true;
size_t count = 0;
for (size_t i = 0; i < s.length(); i++) {
if (s[i] == ' ' || s[i] == '\n' || s[i] == '\t') {
space = true;
}
else if (space == true) {
space = false;
count++;
}
}
return count;
}
/* TODO: write this modified version of wordcount, which stores the
* words in a vector in addition to returning the count. */
/*
size_t wordcount(const string& s, vector<string>& l)
{
bool space = true;
size_t count = 0;
string word;
for (size_t i = 0; i < s.length(); i++) {
if (s[i] == ' ' || s[i] == '\n' || s[i] == '\t') {
space = true;
l.push_back(word);
word = "";
}
else if (space == true) {
space = false;
count++;
}
else if (space == false) {
word += s[i];
}
}
return count;
}
*/
size_t wordcount(const string& s, vector<string>& l)
{
bool justReadSpace = true;
size_t count = 0;
string currentWord;
for (size_t i = 0; i < s.length(); i++) {
if (justReadSpace) {
if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
justReadSpace = false;
currentWord += s[i];
count++;
}
} else {
if (s[i] == ' ' || s[i] == '\n' || s[i] == '\t') {
justReadSpace = true;
l.push_back(currentWord);
currentWord = "";
} else {
currentWord += s[i];
}
}
}
if (currentWord.length() > 0) {
l.push_back(currentWord);
}
return count;
}
int main()
{
/* TODO: test code for the above goes here. */
vector<string> words;
string s = "please count the correct number of words";
size_t count = wordcount(s, words);
cout << "There are "<< count << " words in this string: " << endl;
for (size_t i = 0; i < words.size(); i++) {
cout << words[i] << endl;
}
return 0;
}
/* TODO: (this could be a little challenging...) Try to write
* a program that reads C/C++ source and removes the comments.
* Look at the last diagram from lecture for inspiration.
* */