forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex9_49.cpp
37 lines (32 loc) · 1.01 KB
/
ex9_49.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
//
// ex9_49.cpp
// Exercise 9.49
//
// Created by pezy on 12/5/14.
//
// @Brief A letter has an ascender if, as with d or f, part of the letter extends
// above the middle of the line.
// 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>
#include <iostream>
using std::string; using std::cout; using std::endl; using std::ifstream;
int main()
{
ifstream ifs("../data/letter.txt");
if (!ifs) return -1;
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;
}