ABC words: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
(Added C++ solution) |
||
Line 152: | Line 152: | ||
31 metalwork 32 railway 33 sallow 34 saltwater 35 sandalwood |
31 metalwork 32 railway 33 sallow 34 saltwater 35 sandalwood |
||
36 shadflower 37 shallow 38 stalwart 39 tailwind 40 tallow</pre> |
36 shadflower 37 shallow 38 stalwart 39 tailwind 40 tallow</pre> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <cstdlib> |
|||
#include <fstream> |
|||
#include <iostream> |
|||
bool is_abc_word(const std::string& word) { |
|||
size_t a = std::string::npos; |
|||
size_t b = std::string::npos; |
|||
for (size_t pos = 0, n = word.size(); pos < n; ++pos) { |
|||
switch (word[pos]) { |
|||
case 'a': |
|||
// record position of first 'a' |
|||
if (a == std::string::npos) |
|||
a = pos; |
|||
break; |
|||
case 'b': |
|||
if (b == std::string::npos) { |
|||
// fail if we haven't seen 'a' yet |
|||
if (a == std::string::npos) |
|||
return false; |
|||
// record position of first 'b' |
|||
b = pos; |
|||
} |
|||
break; |
|||
case 'c': |
|||
// succeed iff we've seen 'b' already |
|||
return b != std::string::npos; |
|||
} |
|||
} |
|||
return false; |
|||
} |
|||
int main(int argc, char** argv) { |
|||
const char* filename(argc < 2 ? "unixdict.txt" : argv[1]); |
|||
std::ifstream in(filename); |
|||
if (!in) { |
|||
std::cerr << "Cannot open file '" << filename << "'.\n"; |
|||
return EXIT_FAILURE; |
|||
} |
|||
std::string word; |
|||
int n = 1; |
|||
while (getline(in, word)) { |
|||
if (is_abc_word(word)) |
|||
std::cout << n++ << ": " << word << '\n'; |
|||
} |
|||
return EXIT_SUCCESS; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
1: aback |
|||
2: abacus |
|||
3: abc |
|||
4: abdicate |
|||
5: abduct |
|||
6: abeyance |
|||
7: abject |
|||
8: abreact |
|||
9: abscess |
|||
10: abscissa |
|||
11: abscissae |
|||
12: absence |
|||
13: abstract |
|||
14: abstracter |
|||
15: abstractor |
|||
16: adiabatic |
|||
17: aerobacter |
|||
18: aerobic |
|||
19: albacore |
|||
20: alberich |
|||
21: albrecht |
|||
22: algebraic |
|||
23: alphabetic |
|||
24: ambiance |
|||
25: ambuscade |
|||
26: aminobenzoic |
|||
27: anaerobic |
|||
28: arabic |
|||
29: athabascan |
|||
30: auerbach |
|||
31: diabetic |
|||
32: diabolic |
|||
33: drawback |
|||
34: fabric |
|||
35: fabricate |
|||
36: flashback |
|||
37: halfback |
|||
38: iambic |
|||
39: lampblack |
|||
40: leatherback |
|||
41: metabolic |
|||
42: nabisco |
|||
43: paperback |
|||
44: parabolic |
|||
45: playback |
|||
46: prefabricate |
|||
47: quarterback |
|||
48: razorback |
|||
49: roadblock |
|||
50: sabbatical |
|||
51: snapback |
|||
52: strabismic |
|||
53: syllabic |
|||
54: tabernacle |
|||
55: tablecloth |
|||
</pre> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |