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
|
// -- lefthand2.cpp - Find longest word typed with left hand.
// Fred Swartz - 2002-09-16
// Use a vector to save all words of longest length.
// Note: should use a set because only want one copy.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//--- prototypes
bool isOnlyLeftHand(string w);
//======================================================= main
int main() {
string word; // holds each word as read
vector<string> maxWords; // longest words
int maxlen = 0; // length of longest word
while (cin >> word) {
if (word.length() >= maxlen && isOnlyLeftHand(word)) {
if (word.length() > maxlen) {
maxlen = word.length(); // new max
maxWords.clear(); // clear shorter words
}
//--- Add word only if not already there
//--- Could use find function, but ...
bool add = true;
for (int i=0; i<maxWords.size(); i++) {
if (maxWords[i] == word) {
add = false;
break;
}
}
if (add) {
maxWords.push_back(word); // remember this word
}
}
}
cout << "Words of length " << maxlen << endl;
for (int i=0; i<maxWords.size(); i++) {
cout << maxWords[i] << endl;
}
return 0;
}//end main
//=============================================== isOnlyLeftHand
bool isOnlyLeftHand(string w) {
return w.find_first_not_of("qwertasdfgzxcvb") == string::npos;
}//end isOnlyLeftHand |