练习5.21:修改5.5.1节(第171页)练习题的程序,使其找到的重复单词必须以大写字母开头。
答案:见云盘程序。
练习5.21
/*
*2015/6/7
*练习5.21
*功能:修改5.5.1节(第171页)练习题的程序,使其找到的重复单词必须以大写字母开头
*作者:Nick Feng
*nickgreen23@163.com
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word, nextword,saveword;
int flag = 0;
if (cin >> word)
{
while (cin >> nextword && nextword != "999")
{
if (word == nextword)
{
string &first = word;
toupper(first[0]);
cout << "saveword: " << word << endl;
flag = 1;
break;
}
else {
word = nextword;
flag = 2;
}
}
}
if (flag == 2) cout << "No Finding" << endl;
return 0;
}
练习5.21-2
/*
*练习5.21修正
*2015/6/8 8:49
*说明;昨天题目没看清,题目要求输入的连续重复的单词以大写字母开头才输出,而并非改成第一个字母为大写,完全两个意思
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word, nextword;
int flag = 0;
if (cin >> word)
{
while (cin >> nextword && nextword != "999") //以输入 999 结束
{
if (nextword == word )
{
if (!isupper(word[0]))
//isupper() 参考书上82页 ,判断一个字符是否为大写
//如果不是大写的,继续循环
else
{
cout << "it is: " << word << endl;
// 输出大写开头的连续重复的单词
flag = 1;
break;
}
}
else
{
word = nextword;
flag = 2;
}
}
if (flag == 2) cout << "No finding" << endl;
}
return 0;
}