char数组c ++元音

问题描述 投票:-2回答:1

我正在尝试制作一个使用switch语句的程序,并查看char数组的元素是否为元音,以及哪个是元音,但是我被困在如何检查元素的位置:

int prob2() {
char uName[25] = "";
int voCo = 0;
cout<<"Enter you first and last name, under 25 chars please: ";
cin>>uName;
int i = 0;
while(i <= 25){
switch(i){
    case 1:

    voCo++;
    break;
    case 2:

    voCo++;
    break;
    case 3:

    voCo++;
    break;
    case 4:

    voCo++;
    break;
    case 5:

    voCo++;
    break;
    default:

    break;
}
i++;
}
cout<<"Your first and last name have: "<<voCo<<" vowels in them."<<endl;
return 0;
}
c++ arrays char
1个回答
0
投票

您可以尝试这样的事情:

const std::string vowels = "aeiou";

const std::string name = "martin luther king, jr.";
const unsigned int name_length = name.length();
unsigned int vowel_count = 0U;
for (unsigned int i = 0U; i < name_length; ++i)
{
  if (vowels.find(name[i]) != std::string::npos)
  {
    ++vowel_count;
  }
}

不需要switch语句。这是许多可能的算法或实现之一。

© www.soinside.com 2019 - 2024. All rights reserved.