题目
如果一个字母延伸到中线之上,如d或f,则称其有上出头部分(ascevder)。如果一个字母延伸到中线之下,如p或g,则称其有下出头部分(descender)。编写程序,读入一个单词文件,输出最长的既不包含上出头部分,也不包含下出头部分的单词。
代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
const string out_top = "bdfhklt", out_buttom = "gjpq";
ifstream in("fstreamtest.txt"); //读入文件
string temp, ans;
while (in >> temp) {
//排除上出头单词
if (temp.find_first_of(out_top) != string::npos) continue;
//排除下出头单词
if (temp.find_first_of(out_buttom) != string::npos) continue;
if (temp.size() > ans.size()) {
ans = temp;
}
}
cout << ans;
return 0;
}
这里txt文件中单词以空格隔开,所以输入方式用in>>temp;如果每个单词占一行的话,可以用getline(in,temp);