C++ show vowel and consonant and count it

前端 未结 3 1067
南旧
南旧 2021-01-26 01:47

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.

Example: U

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-26 02:19

    #include 
    using namespace std;
    
    /**
     * Checks if the given letter is a vowel.
     */
    bool isVowel(char let) {
        return let == 'a' || let == 'e' || let == 'i' || let == 'o' || let == 'u';
    }
    
    /**
     * Returns the character for the given int.
     */
    char toChar(int num) {
        return (char) ('a' + num - 1);
    }
    
    int main (void) {
        int vow = 0,
            con = 0,
            num,
            i;
        char let;
        cout << "Please enter a number: ";
        cin >> num;
        for (i = 1; i <= num; ++i) {
            let = toChar(i);
            if (isVowel(let)) vow++;
            else con++;
        }
        cout << "The letter was \"" << let
             << "\" and there were " << vow
             << " vowels and " << con
             << " consonants." << endl;
        return 0;
    }
    

提交回复
热议问题