繁体   English   中英

字符串中的元音数量C ++

[英]Number of vowels in string of characters C++

我正在尝试编写一个程序,要求用户提示输入一个字符序列。 它可以是一个单词或整个句子。 我编写了代码,当它是一个单词时,它正确地计算了元音的数量,但是我将如何编写它以便在整个句子中计算元音?

我现在拥有的代码:

int main() {
    // Write your main here
    // var
    string word;
    int sum = 0;
    int count = 0;

    // Prompt user to enter a character
    cout << "Enter a sequence of characters: " << endl;
    cin >> word;
    cout << endl;

    int length = word.length();
    for(int i = 0; i < length; i++)
    {
        if (isVowel(word[i]))
        {
            sum += 1;
            count++;
        } // if
    } // for loop

    cout << "There are " << sum << " vowels." << endl;

    return 0;
}

bool isVowel(char letter) {
    switch (letter)
    {
        case 'A' :
        case 'a' :
        case 'E' :
        case 'e' :
        case 'I' :
        case 'i' :
        case 'O' :
        case 'o' :
        case 'U' :
        case 'u' :
            return true;
        default:
            return false;
    }
}

现在,如果我输入:这是一个句子。 输出将是:有1个元音。 我知道我必须将字符串单词更改为char单词,但是然后我不知道如何用它来计算元音的数量。

您可以轻松地更改cin >> word; getline(cin, word); 它在C ++标准库中。 它将读取您的输入,直到第一行。 (输入)

有多种方法可以解决此问题。 为了能够用多个单词找到元音,您必须首先阅读多个单词作为输入。 cin在遇到第一个空格时停止读取字符。 因此,使用cin >> word不能读取多个cin >> word 而是使用getline读取整文本,例如

    std::string line;                   /* string to hold each line */
    ...
    std::cout << "enter characters: ";  /* prompt, read, validate line */
    if (!getline (std::cin, line)) {
        std::cerr << "error: input failure.\n";
        return 1;
    }

阅读整行文本后,您可以使用自动调整范围的for循环遍历每个字符,以检查每个字符是否为元音,或者可以使用C ++提供的其他一些工具来帮助您计算元音。 .find_first_of()允许在字符串的指定集中查找第一个字符。 参见cppreference std :: string-find_first_of 方便地, find_first_of返回原始字符串中找到匹配字符的位置,该位置可以递增并用于查找下一个元音,例如

    const std::string vowels = "AEIOUaeiou";  /* vowels */
    size_t n = 0, nvowels = 0;          /* positon and vowel counts */
    ...
    /* loop until vowel not found in remaining substring */
    while ((n = line.find_first_of (vowels, n)) != std::string::npos) {
        nvowels++;  /* increment vowel count */
        n++;        /* increment n to one past current vowel */
    }

注意: .find_first_of()返回的字符串'n'内的偏移量会增加+1以索引下一个调用在当前元音之后的下一个字符)

添加输出以标识每个元音以及在输入字符串中找到该元音的位置,您可以执行以下操作:

#include <iostream>
#include <iomanip>
#include <string>

int main (void) {

    std::string line;                   /* string to hold each line */
    const std::string vowels = "AEIOUaeiou";  /* vowels */
    size_t n = 0, nvowels = 0;          /* positon and vowel counts */

    std::cout << "enter characters: ";  /* prompt, read, validate line */
    if (!getline (std::cin, line)) {
        std::cerr << "error: input failure.\n";
        return 1;
    }
    std::cout << '\n';

    /* loop until vowel not found in remaining substring */
    while ((n = line.find_first_of (vowels, n)) != std::string::npos) {
        nvowels++;  /* increment vowel count */
        std::cout << "  vowel[" << std::setw(2) << nvowels << "] '" << 
                    line[n] << "' at " << n << '\n';
        n++;        /* increment n to one past current vowel */
    }

    std::cout << "\n" << nvowels << " vowels in input string.\n";
}

使用/输出示例

$ ./bin/vowel_find_first_of
enter characters: A quick brown fox jumps over the lazy dog

  vowel[ 1] 'A' at 0
  vowel[ 2] 'u' at 3
  vowel[ 3] 'i' at 4
  vowel[ 4] 'o' at 10
  vowel[ 5] 'o' at 15
  vowel[ 6] 'u' at 19
  vowel[ 7] 'o' at 24
  vowel[ 8] 'e' at 26
  vowel[ 9] 'e' at 31
  vowel[10] 'a' at 34
  vowel[11] 'o' at 39

11 vowels in input string.

大概有六种方法可以将C ++提供的工具组合在一起以计算元音。 这只是想到的 第一 秒钟,毫无疑问,有更好的方法可以做到这一点。

仔细看一下,如果您有任何问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM