C++ Primer(第5版) 练习 5.12
练习 5.12 修改统计元音字母的程序,使其能统计以下含有两个字符的字符序列的数量:ff、fl和fi。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************> File Name: ex5.11.cpp> Author: > Mail: > Created Time: Sun 11 Feb 2024 12:48:36 PM CST************************************************************************/#include<iostream>
#include<vector>
using namespace std;int main(){unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;unsigned space = 0, tab = 0, newline = 0;unsigned ff = 0, fl = 0, fi = 0;string ch;while(getline(cin, ch)){++newline;for(auto s = ch.begin(); s != ch.end(); s++){switch(*s){case 'a':case 'A': ++aCnt; break;case 'e':case 'E': ++eCnt; break;case 'i':case 'I': ++iCnt; break;case 'o':case 'O': ++oCnt; break;case 'u':case 'U': ++uCnt; break;case ' ': ++space; break;case '\t': ++tab; break;default: break;}}for(auto s = ch.begin(); s != ch.end(); s++){if(*s == 'f'){if(*(s + 1) == 'f'){ff++;}else if(*(s + 1) == 'l'){fl++;}else if(*(s + 1) == 'i'){fi++;}}}}cout<<"Number of vowel a, A:\t"<<aCnt<<endl;cout<<"Number of vowel e, E:\t"<<eCnt<<endl;cout<<"Number of vowel i, I:\t"<<iCnt<<endl;cout<<"Number of vowel o, O:\t"<<oCnt<<endl;cout<<"Number of vowel u, U:\t"<<uCnt<<endl;cout<<"Number of space:\t"<<space<<endl;cout<<"Number of tab:\t\t"<<tab<<endl;cout<<"Number of newline:\t"<<newline<<endl;cout<<"Number of ff:\t\t"<<ff<<endl;cout<<"Number of fl:\t\t"<<fl<<endl;cout<<"Number of fi:\t\t"<<fi<<endl;return 0;
}