C ++显示元音和辅音并计数 [英] C++ show vowel and consonant and count it

查看:113
本文介绍了C ++显示元音和辅音并计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入1到26之间的数字(表示az)时,如何显示字母并计算其中的元音和辅音数量.

When user input number from 1 - 26 (which mean a to z), how to show the the letter and count how many vowel and consonant inside it.

示例: 用户输入= 13 然后将显示= 一种 b C d Ë F G H 一世 Ĵ ķ 升 米 元音= 3 辅音= 10

Example: Users input=13 Then will show = a b c d e f g h i j k l m Vowel = 3 Consonant = 10

我刚才如何计算不显示它

i just now how to count it not to show it

#include <iostream>
using namespace std;
int main (){
int vow=0, con=0;
char let;
.........
.........
if (let=='a' || let=='e' || let=='i' || let=='o' || let=='u'){vow++}
else{con++}
cout<<"Vowel = "<<vow<<endl;
cout<<"Consonant"<<con<<endl;
}

推荐答案

要显示字母,可以使用以char为索引的for循环.

To show the letters, you can use a for loop, using a char as index.

int n = 13;
unsigned int vowel = 0;
unsigned int consonant = 0;
int a = (int)'a';
for (char letter = 'a'; (int)letter < a + n; letter++) {
    cout << letter << " ";
    if (is_vowel(letter)) vowel++;
    else consonant++;
}
cout << std::endl << "vowels: "<< vowel << " consonants: " << consonant << std::endl;

因此,您必须实现is_vowel方法.

So, you must implement the is_vowel method.

这篇关于C ++显示元音和辅音并计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆