到目前为止,我怎么能数出一串中有多少个辅音呢

how can i count how many consonants are in a string so far i have this

本文关键字:一串 多少 怎么能 到目前为止      更新时间:2023-10-16
int countConsonant(string str, int consonant)
{
    int length = str.length();
     consonant = 0;
        for (int i = 0; i < length; i++)
        {
            if(str[i] == 'b'&& str[i] == 'c' && str[i] == 'd'&& str[i] == 'f'
                && str[i] == 'g'&& str[i] == 'h'&& str[i] == 'j' && str[i] == 'k'&& str[i] == 'l'&& str[i] == 'm'
                && str[i] == 'n'&& str[i] == 'p'&& str[i] == 'q'&& str[i] == 'r'&& str[i] == 's'&& str[i] == 't'
                && str[i] == 'v'&& str[i] == 'w'&& str[i] == 'x'&& str[i] == 'y'&& str[i] == 'z')
                consonant = consonant + 1;
        }
        return consonant;
}

你很接近,但应该检查逻辑或||,而不是逻辑和&&str[i]不能等于两个不同的东西)。

C++03标准允许您使用关键字andor以及notxor等,而不是这些神秘的(针对新程序员的)符号,但这并没有广泛流行起来——也许是因为微软的编译器在这方面没有默认为符合标准——大概是为了避免破坏现有的客户端代码,类型等。因此,为了便于携带和简化,许多图书馆和教科书也避免使用这些关键字。

另一种可能更简洁的方法是使用<cctype>中的isalpha(),然后检查它不是元音。更快的方法倾向于使用从字符值到bool的数组,但要注意由于有符号字符值或>=128位非ASCII值而在数组外进行索引。如果还需要考虑大写/小写,那么在测试字符(即char c = tolower(str[i])); if (c == '...)之前,您可能需要在字符上使用tolower()

其他注意事项:您的功能应该:

  • 通过const引用(即const std::string& str)接受其std::string参数,以避免将值从调用上下文复制到该函数本地的单独变量中,这是不必要且耗时的。复制不会对功能造成任何真正的损害,但没有必要
  • consonant设为局部变量,而不是函数参数,因为任何输入值都会立即被0替换,结果由函数返回,而不是写入consonant(这是不可能的,因为它是通过值传递的,而不是通过指针/引用传递的)

这个怎么样:

#include <algorithm>
#include <iostream>
#include <string>

bool isVowel(char c)
{
    switch (c)
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        {
            return true;
        }
        default:
        {
            return false;
        }
    }
}

bool isConsonant(char c)
{
    return !isVowel(c);
}

int main(int argc, char *argv[])
{
    std::string test = "hello";
    std::size_t numVowels = std::count_if(test.begin(), test.end(), isConsonant);
    std::cout << "Number of vowels is: " << numVowels << std::endl;
    return 0;
}

首先:&&将测试所有,直到返回false或到达终点。您想要的是||,它将进行测试,直到条件为true
第二:什么是更短的?测试辅音还是测试元音?当然是元音。只需将测试放在一个额外的函数中(注意:我假设这里有一个有效的按字母顺序输入的字符串):

function is_vowel(c) : bool
  for each vowel test
    if c == that vowel
      return true
  return false

之后,只需将您的大条件语句替换为一个简单的!is_vowel(str[i]).:)最后但同样重要的是,您希望递增您的consonant变量,并且有一个特殊的运算符:递增运算符!(很酷的名字,是吗?)

++consonant; // better than consonant = consonant + 1; but same result

查看正则表达式。它允许您指定要检查的字符列表。您可以对照列表检查字符串中的每个字符,并递增计数器。

http://www.johndcook.com/cpp_regex.html

对于初学者,您使用的是&&(和),在这种情况下这是一个逻辑错误。您要使用||(或)。

您还可以通过检查代码是否不是元音来大大缩短代码。

int countConsonant(string str, int consonant)
{
    int length = str.length();
    int consonant = 0;
    for (int i = 0; i < length; i++)
    {
        if(!(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' ||
            str[i] == 'u'))
        consonant = consonant + 1;
    }
    return consonant;
}

其他人已经发布了该代码的另一种形式作为函数,您只需在其中传递要测试的变量。

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
size_t countConsonant(const string& s) {
    const string vowels("aeiou");
    size_t count = 0;
    for (string::const_iterator i = s.begin(); i != s.end(); ++i) {
        if (vowels.find(tolower(*i)) == string::npos) {
            ++count;
        }
    }
    return count;
}
int main() {
    cout << countConsonant("abcd") << endl;
}

或者,如果你不想让非阿尔法的东西匹配,你可以这样做:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
size_t countConsonant(const string& s) {
    const string cons("bcdfghjklmnpqrstvwxyz");
    size_t count = 0;
    for (string::const_iterator i = s.begin(); i != s.end(); ++i) {
        if (cons.find(tolower(*i)) != string::npos) {
            ++count;
        }
    }
    return count;
}
int main() {
    cout << countConsonant("abcd") << endl;
}

这篇作业太夸张了,但仅供参考-一种面向对象的方法:

struct Consonants
{
    bool x_[256];
    Consonants()
    {
        // constructor sets x_[i] to true or false to indicate
        // whether character with ASCII number i is a consonant...
        for (int i = 0; i < 256; ++i)
            x_ = false;
        // note: ASCIIZ string finishes with NUL, so *p becomes false
        //       and for loop exits
        for (const char* p = "bcdfghjklmnpqrstvwxyz"
                             "BCDFGHJKLMNPQRSTVWXYZ"; *p; ++p)
            x_[*p] = true;
    };
    int in(const std::string& str) const
    {
        int result = 0;
        for (int i = 0; i < str.size(); ++i)
            if (x_[(unsigned char)str[i]])
                ++result;
        return result;
    }
};

用法:

Consonants consonants;  // create an "utility" object once
int c1 = consonants.in("hello world");   // use as often as desired
int c2 = consonants.in("goodbye cruel world");

您可以使用以下内容来计算字符串中的辅音数量:

int main()
{
    char a[20];
    int consonants; 
    gets(a);
    cout<<a<<endl;
    if(a[i]>=0 && a[i]<=9)  //  consonants
    {
        consonants++;
    }
    cout<<"Total Consonats are: "<<consonants;
}