1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 找出元音字母a e i o u出现的次数

找出元音字母a e i o u出现的次数

时间:2021-05-08 20:18:26

相关推荐

找出元音字母a e i o u出现的次数

利用getline函数的两种用法cin.getline()输入流在外面getline()输入流作为参数保存在里面第一种方法:利用c++primer里面提供的,使用基于范围for语句。#include<iostream>#include<string>using namespace std;int main() {string s; int a = 0, b = 0, cd = 0, d = 0, e = 0;getline(cin, s);for (auto c:s) {switch (c) {case '\0':break;case 'a':++a; break;case 'e':++b; break;case 'i':++cd; break;case 'u':++d; break;case 'o':++e; break;}}cout << a << " " << b << " " << cd << " " << e << " " << d << endl;return 0;}

第二种利用数组,简单的for循环

#include<iostream>

#include<string>

using namespace std;

int main(){

char s[80]; int a=0,b=0,c=0,d=0,e=0;

cin.getline(s,80);

for(int i=0;i<80;++i){

switch(s[i]){

case '\0':

break;

case 'a' :

++a;break;

case 'e' :

++b;break;

case 'i' :

++c;break;

case 'u':

++d;break;

case 'o':

++e;break;

}

}

cout << a << " "<<b <<" "<<c<<" " <<e<<" "<<d<<endl;

return 0;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。