简体   繁体   English

C ++将Char与字符串进行比较

[英]C++ comparing Char to Strings

I'm trying to loop through a word but only vowels are working for some reason. 我试图循环说一句话,但只有元音因某种原因而起作用。 The other type prints out the amount of words in total. 另一种类型打印出总数量的单词。 Please excuse my poor choise of words mixed with Swedish and English. 请原谅我用瑞典语和英语混合的单词。

#include <iostream>

using namespace std;

int main()
{
    int vo = 0;
    int ko = 0;

    char vocals[7]
    {
        'A','O','U','E','I','Y'
    };


    char konsonanter[19]
    {
        'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','X','Z'
    };

    string word = "Toysoldier";

    for(int i = 0; i < word.length(); i++)
    {
        for(int v = 0; v < vocals[v]; v++)
        {
            if(vocals[v] == word[i])
                vo++;
        }

        for(int k = 0; k < konsonanter[k]; k++)
        {
            if(konsonanter[k] == word[i])
                ko++;
        }
    }


    cout << "Konsonanter = " << ko << " Vokaler = " << vo << endl;
}

You got your loop condition wrong. 你的循环条件错了。 Instead of v < vocals[v] you probably meant to write v < 7 as there are 7 vocals in your array. 而不是v < vocals[v]你可能打算写v < 7因为你的数组中有7个人声。

Writing v < vocals[v] is not an error as v is initialized to zero while the expression vocals[v] is a char which can also be used in array subscripts ('A' is translated to 65, 'E' to 69 and so on). v < vocals[v]不是错误,因为v初始化为零,而表达式vocals[v]是一个char ,也可以在数组下标中使用('A'转换为65,'E'转换为69和等等)。

The same problem exists in the loop for consonants. 辅音循环中存在同样的问题。

You have more than one bug, so I'll just remark on all the code in order. 您有多个错误,所以我只是按顺序注释所有代码。

If you write 如果你写

char vocals[7] = {'A' ... 'Y'};

you have to manually keep the array size and the initializer in sync - here, for example, you missed one (you have 6 "vocals" in your initializer for a 7-element array). 你必须手动保持数组大小和初始化程序同步 - 例如,你错过了一个(你的初始化程序中有7个元素的6个“人声”)。

It's less error-prone to write 写入错误的可能性较小

char vowels[] = {'A', 'E', 'I', 'O', 'U'};

and let the compiler count it for you. 让编译器为你计算它。 (I renamed it to "vowels" because that's what they're called in English, and removed 'Y' because it's not one - obviously you can stick with whatever works in Swedish.) (我把它改名为“元音”,因为这就是他们用英语调用的东西,并删除了'Y',因为它不是一个 - 显然你可以坚持使用瑞典语中的任何作品。)

Similarly, this loop is wrong: 同样,这个循环是错误的:

for(int v = 0; v < vocals[v]; v++)

because you're comparing v (an index into your array) with vocals[v] which is a character value. 因为你将v (你的数组中的索引)与作为字符值的vocals[v]进行比较。 They're two different types of thing, and comparing doesn't make sense here. 它们是两种不同类型的东西,比较在这里没有意义。 The loop over an array would normally be written 通常会写入数组上的循环

for (int v = 0; v < sizeof(vocals)/sizeof(vocals[0]); ++v)

so for some N-element array, the index v ranges from 0 to N-1. 因此对于某些N元素阵列,索引v范围从0到N-1。 A simpler way is to just write 一种更简单的方法就是写

for (auto v : vocals)

where v is now a character, taking the value of each vocal/vowel one-by-one in the loop body. 其中v现在是一个字符,在循环体中逐个获取每个声音/元音的值。 The compiler can figure out the array size for you. 编译器可以为您计算出数组大小。

I'd honestly re-write all your loops in this style: you don't need to manage array indices and bounds manually unless you're doing something more complicated. 老老实实地用这种风格重写所有循环:你不需要手动管理数组索引和边界,除非你做的更复杂。

On top of the loop condition bugs, your character comparisons will anyway mostly fail: the vowel/vocal and consonant arrays both contain only upper-case letters, and your string is mostly lower-case. 在循环条件错误之上,你的角色比较无论如何都会失败:元音/声音和辅音数组都只包含大写字母,而你的字符串大多是小写的。 You need to manage this, for example with 你需要管理它,例如

#include <cctype>

// ...
for (auto c : word) {
  for (auto v : vowels) {
    if (v == std::toupper(c)) {

Your error is in the tests used in the "for" loops: v < vocals[v] will compare the integer v to the integer value of the character vocals[v] , which happens to be the ASCII code of that character (in practice, that'll be something between 65 and 90, look up "Ascii table" on the web). 您的错误出现在“for”循环中使用的测试中: v < vocals[v]将整数v与字符vocals[v]的整数值进行比较,这恰好是该字符的ASCII代码(在实践中) ,这将是65到90之间的东西,在网络上查找“Ascii表”。

To fix your code, you should modify your "for" loops like this: 要修复代码,您应该像这样修改“for”循环:

for (char vocal : vocals) {
  if (word[i] == vocal) vo++;
}
for (char konsonant : konsonanter) {
  if (word[i] == vocal) ko++;
}

Also, note that you got the number of vocals wrong in your "vocals" array: it's 6, not 7. 另外,请注意你的“vocals”数组中的人声数量是错误的:它是6,而不是7。

You conditions in the loops are flawed, you should use 'for (int v = 0; v < sizeof(vocals); v++)`. 你在循环中的条件是有缺陷的,你应该使用'for(int v = 0; v <sizeof(vocals); v ++)`。

Also, check your condition for a vocal and consonant: What happens if you consider lowercase chars? 另外,检查你的声音和辅音的条件:如果你考虑小写字符怎么办? 'a' != 'A' , so you don't count them correctly. 'a' != 'A' ,所以你不能正确计算它们。 Hint: Use toupper() or tolower() so you don't need to have excessive lists of chars. 提示:使用toupper()tolower()因此您不需要有过多的字符列表。

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

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