Skip to content

Commit

Permalink
Update ex9_49.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Jun 24, 2015
1 parent d7ea268 commit 8c3856f
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions ch09/ex9_49.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// A letter has a descender if, as with p or g, part of the letter extends below the line.
// Write a program that reads a file containing words and reports the longest word
// that contains neither ascenders nor descenders.
//
// @Refactor @Yue Wang Jun 2015
//

#include <string>
#include <fstream>
Expand All @@ -21,16 +24,15 @@ int main()
{
ifstream ifs("../data/letter.txt");
if (!ifs) return -1;
string longest_word;
for (string word; ifs >> word; )
if (word.find_first_not_of("aceimnorsuvwxz") == string::npos &&
word.size() > longest_word.size())
longest_word = word;

cout << longest_word << endl;

ifs.close();

string longest;
auto update_with = [&longest](string const& curr)
{
if (string::npos == curr.find_first_not_of("aceimnorsuvwxz"))
longest = longest.size() < curr.size() ? curr : longest;
};
for (string curr; ifs >> curr; update_with(curr));
cout << longest << endl;

return 0;
}

0 comments on commit 8c3856f

Please sign in to comment.