练习5.9
#include <iostream>
int main()
{
using namespace std;
char a;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
while (cin >> a)
{
if ((a == 'a') || (a == 'A'))
++aCnt;
else
{
if ((a == 'e') || (a == 'E'))
++eCnt;
else
{
if ((a == 'I') || (a == 'i'))
++iCnt;
else
{
if ((a == 'O') || (a == 'o'))
++oCnt;
else
{
if ((a == 'U') || (a == 'u'))
++uCnt;
}
}
}
}
}
cout << "The number of a or A is " << " " << aCnt << endl;
cout << "The number of e or E is " << " " << eCnt << endl;
cout << "The number of i or I is " << " " << iCnt << endl;
cout << "The number of o or O is " << " " << oCnt << endl;
cout << "The number of u or U is " << " " << uCnt << endl;
}
练习5.10
同上
练习5.11
略
练习5.12
#include <iostream>
int main()
{
using namespace std;
char a;
int value = 0;//用value来判断上一个输入的字符是否的f,以此来决定是否判断是否需要判断下一个字符是否是f,i,l;
int ffCnt = 0, flCnt = 0, fiCnt = 0;
while (cin >> a)
{
if (value == 0)//上一个字符不是f
{
if (a == 'f')
{
value = 1;
}
else
value = 0;
}
else//上一个字符已经是f,判断当前字符是不是我们需要的
{
if (a == 'f')
{
++ffCnt;
value = 0;
}
if (a == 'i')
{
++fiCnt;
value = 0;
}
if (a == 'l')
{
++flCnt;
value = 0;
}
}
}
cout << "The number of ffCnt is " << " " << ffCnt << endl;
cout << "The number of flCnt is " << " " << flCnt << endl;
cout << "The number of fiCnt is " << " " << fiCnt << endl;
}