计算字符串中的元音元音和辅音均数的程序

时间:2014-12-11 16:23:09

标签: c++ string

您好我不知道如何修复我的程序,以查找我的字符串中有多少个evens,odds,元音和辅音。

该程序编译并运行但我从未得到任何元音或辅音,其他所有内容都以平均值或赔率(甚至是字符)添加。

编辑1:通过平均值和赔率我的意思是,如果用户输入字符串 John123 我想在这种情况下找到多少个字符是元音 1 ' o '在这种情况下有多少辅音 3 ' J ',' h ',' n '在这种情况下有多少是平均值 1 ' 2 '以及在这种情况下赔率是多少 2 ' 1 '和' 3 '。

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string s1;
    int evens = 0; //
    int odds = 0; // 
    int vowels = 0; //
    int consonants  = 0; // 
    cout << "Please type in Something: ";
    getline(cin, s1);
    cout << "You typed: " << s1 << endl;

    for(int i = 0; i < s1.size(); ++i)
    {
        if((s1[i] % 2) == 0 ) 
        {
            ++evens;
        }
        else if((s1[i] % 2 ) != 0) // What would an algorithm (formula) be to find when a number is odd without the not-equals != ?
        {
            ++odds;
        }

        else if(s1[i] == 'A' || 'a' || 'E' || 'e' || 'O' || 'o' || 'I' || 'i' || 'U' || 'u')
        {
            ++vowels;
        }
        else if(s1[i] == 'Q' || 'W' || 'R' || 'T' || 'Y' || 'P' || 'S' || 'D' || 'F' || 'G' || 'H' || 'J' || 'K' || 'L' || 'Z' || 'X' || 'C' || 'V' || 'B' || 'N' || 'M'||
                         'q' || 'w' || 'r' || 't' || 'y' || 'p' || 's' || 'd' || 'f' || 'g' || 'h' || 'j' || 'k' || 'l' || 'z' || 'x' || 'c' || 'v' || 'b' || 'n' || 'm')
 //   I specify every letter so that it dosn't pick whitespaces, symbols etc.
        {
            ++consonants;
        }
    }
    cout << "Evens in string are: " << evens << "\nOdds in string are: " << odds << "\nVowels in string are: " << vowels << "\nConsonants in string are: " << consonants;

    return 0;
}

6 个答案:

答案 0 :(得分:3)

if((s1[i] % 2) == 0 )

此代码不正确。你正在调整角色,这不是你想要的。您想首先使用atoi或类似的东西将字符转换为整数,我无法记住该函数。 (只需查看字符串到int c ++)。然后,一旦您将字符转换为整数,就可以正确调整它。

此外,正如Jongware所提到的,您的||语法完全错误。您需要对每个角色进行==比较。

应该看起来像这样(很长):

(s1[i] == 'A' || s1[i] == 'a' || s1[i] == 'E' || s1[i] == 'e' || s1[i] == 'O' || s1[i] == 'o' || s1[i] == 'I' || s1[i] == 'i' || s1[i] == 'U' || s1[i] == 'u')

您可能希望事先将s1[i]存储在字符中,以便更简洁。

你应该在奇数/偶数测试之前进行角色测试,这样你就不会意外地尝试将h或其他非数字字符转换为整数。

答案 1 :(得分:3)

char是一个数字。例如,a(在大多数计算机上)是ASCII码97.因此,您的偶数测试或奇数测试将始终匹配。您需要重新排列if块。

此外,你的角色测试不符合你的意图。只要s1[i] == 'A' || 'a'等于s1[i],或'A'数字不为零,'a'就会为真。但97永远不会为零,所以测试总是会产生true。您需要的代码更像s1[i] == 'A' || s1[i] == 'a',每次都会测试s1[i]值。

(使事情更简单的一种方法:一旦你知道这个字符不是元音,就可以测试((s1[i] >= 'a' && s1[i] <= 'z') || (s1[i] >= 'A' && s1[i] <= 'Z'))的ASCII辅音。)

答案 2 :(得分:2)

你的||语法没有意义,你现在拥有的是这样的

else if(s1[i] == 'A' || 'a' || 'E' || 'e' || 'O' || 'o' || 'I' || 'i' || 'U' || 'u')

虽然它应该是这样的:

else if(s1[i]=='A' || s1[i]=='a' || s1[i]=='E' || s1[i]=='e' || s1[i]=='O' || s1[i]=='o' || s1[i]=='I' || s1[i]=='i' || s1[i]=='U' || s1[i]=='u')

答案 3 :(得分:2)

你基本上有两个问题:

  1. 考虑到字符代码,字符被解释为数字。当您检查字符是偶数还是奇数时,您正在检查其字符代码,它始终是奇数事件,这就是您的代码将始终进入第一个或第二个案例的原因。你必须检查你的角色是否是数字,如果是,你必须检查它是否是偶数

  2. 您的OR不正确,请参阅Ruby van Soelen的回答

  3. 建议的,未经测试的代码:

    for(int i = 0; i < s1.size(); ++i)
    {
        if (isdigit(s1[i])) {
            if ((s1[i] == '0') || (s1[i] == '2') || (s1[i] == '4') || (s1[i] == '6') || (s1[i] == '8')) 
            {
                ++evens;
            }
            else // What would an algorithm (formula) be to find when a number is odd without the not-equals != ?
            {
                ++odds;
            }
        }
        else 
        {
    
            if(s1[i]=='A' || s1[i]=='a' || s1[i]=='E' || s1[i]=='e' || s1[i]=='O' || s1[i]=='o' || s1[i]=='I' || s1[i]=='i' || s1[i]=='U' || s1[i]=='u')
            {
                ++vowels;
            }
            else if(s1[i]=='B' || s1[i]=='b' || s1[i]=='C' || s1[i]=='c' || s1[i]=='D' || s1[i]=='d' || s1[i]=='F' || s1[i]=='f' || s1[i]=='G' || s1[i]=='g' || s1[i]=='H' || s1[i]=='h' || s1[i]=='J' || s1[i]=='j' || s1[i]=='K' || s1[i]=='k' || s1[i]=='L' || s1[i]=='l' || s1[i]=='M' || s1[i]=='m' || s1[i]=='N' || s1[i]=='n' || s1[i]=='P' || s1[i]=='p' || s1[i]=='Q' || s1[i]=='q' || s1[i]=='R' || s1[i]=='r' || s1[i]=='S' || s1[i]=='s' || s1[i]=='T' || s1[i]=='t' || s1[i]=='V' || s1[i]=='v' || s1[i]=='X' || s1[i]=='x' || s1[i]=='Y' || s1[i]=='y' || s1[i]=='Z' || s1[i]=='z')
     //   I specify every letter so that it dosn't pick whitespaces, symbols etc.
            {
                ++consonants;
            }
        }
    }
    

答案 4 :(得分:1)

else-if语句中的表达式将始终等于true

else if(s1[i] == 'A' || 'a' || 'E' || 'e' || 'O' || 'o' || 'I' || 'i' || 'U' || 'u')

因为它相当于

( s1[i] == 'A'  ) || 'a' || 'B' /*...*/

其中&#39; a&#39;因为它不等于零,所以评估为真。

所以你必须写至少像

s1[i] == 'A' || s1[i] == 'a' || s1[i] == 'B' /*...*/

还要考虑到例如字符'B'虽然不是数字但其代码是偶数。因此,如果从语句

开始检查,您将得到错误的结果
if((s1[i] % 2) == 0 ) 

您应首先检查该字符是否为数字。您还可以定义一个元音和辅音数组,并使用标准函数strchr来检查一个字符是元音还是辅音。

例如

#include <iostream>
#include <string>
#include <cctype>

//...

const char *alpha_vowels = "AEIOU";
const char &alpha_consonants = "BCD...";

//...

if ( std::isdigit( s[i] ) )
{
    s[i] % 2 == 0 ? ++evens : ++odds;
}
else if ( std::strchr( alpha_vowels, std::toupper( ( unsigned char )s[i] ) ) != nullptr )
{
    ++vowels;
}
else if ( std::strchr( alpha_consonants, std::toupper( ( unsigned char )s[i] ) ) != nullptr )
{
    ++consonants;
}

答案 5 :(得分:1)

除了其他答案之外,这是完成同样事情的更简单方法。

std::set<char> vs{'a', 'o', 'u', 'e', 'i'};
for (auto&& c : s1) {
    if (std::isdigit(c))
        if ((c - '0') % 2 == 0) // Trick to convert numeric char -> int.
            ++evens;
        else
            ++odds;
    else if (std::isalpha(c))
        if (vs.count(std::tolower(c)))
            ++vowels;
        else
            ++consonants;
}