Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 元音的数量,包括;“是”;_C++ - Fatal编程技术网

C++ 元音的数量,包括;“是”;

C++ 元音的数量,包括;“是”;,c++,C++,我必须编写一个函数,输出给定字符串行中的所有元音,包括Y,除非Y后面跟着另一个元音 #include <iostream> #include <string> using namespace std; int numVowelsIncludingY(string line){ int v = 0; for(int i = 0; i < line.length(); ++i){ if(line[i] == 'A' || line[i] == 'E'

我必须编写一个函数,输出给定字符串行中的所有元音,包括Y,除非Y后面跟着另一个元音

#include <iostream>
#include <string>
using namespace std;

int numVowelsIncludingY(string line){
  int v = 0;

  for(int i = 0; i < line.length(); ++i){
    if(line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U' || line[i] == 'Y' ||
       line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'y'){
        ++v;
    }
  }
  return v;
}
例如:

"test phrase" returns 3
"yes no why not?" returns 4
我尝试过使用getline(),但我不确定是否有一种方法可以在不需要用户输入的情况下使用它。我需要它使用一个已经声明的字符串

这是我的代码,虽然有些有效,但需要输入

#include <iostream>
#include <string>
using namespace std;

int numVowelsIncludingY(string line){
  int v = 0;
  getline(cin, line);

  for(int i = 0; i < line.length(); ++i){
    if(line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U' || line[i] == 'Y' ||
       line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'y'){
        ++v;
    }
  }
  return v;
}
#包括
#包括
使用名称空间std;
int NUMVOWELINCLUDIGY(字符串行){
int v=0;
getline(cin,line);
对于(int i=0;i
更新

我已经删除了getline,阅读字符串可以很好地工作,但是我仍然不知道如何将Y作为一个元音来计算,如果它后面没有另一个元音

#include <iostream>
#include <string>
using namespace std;

int numVowelsIncludingY(string line){
  int v = 0;

  for(int i = 0; i < line.length(); ++i){
    if(line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U' || line[i] == 'Y' ||
       line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'y'){
        ++v;
    }
  }
  return v;
}
#包括
#包括
使用名称空间std;
int NUMVOWELINCLUDIGY(字符串行){
int v=0;
对于(int i=0;i
当您将字符串作为参数传递时,不需要在函数内部使用
getline
。尽管您可以从某个流中读取函数外部的字符串。例如,要使用不同的输入进行测试,您可以编写:

void test(std::istream& input_stream) {
    std::string line;
    std::getline(input_stream,line);
    std::cout << line << ": " << numVowelsIncludingY(line) << "\n";
}

int main(){
    std::stringstream test1{"test phrase"};
    test(test1);
    std::stringstream test2{"yes no why not?"};
    test(test2);
    std::stringstream test3{"ya"};
    test(test3);
    std::stringstream test4("aa");
    test(test4);
}
test
也可以通过调用
std::cin
来读取用户的输入

对于
Y
后面没有元音的特殊情况,您需要注意边界,以避免越界访问。我建议为最后一个字符额外花费一行代码,然后我们可以为主要部分断言每个字符都有下一个字符

因为我们需要至少两次检查一个字符是否是元音,所以我决定使用lambda表达式,在我使用它的同时,我还编写了一个表达式来检查
Y
Y

有很多方法可以检查字符是否是元音。我认为在字符串<代码>“AiououaIo”< /COD>中查找字符是简单的。当找不到字符时,code>std::string::find
返回
std::string::npos
,否则我们知道它是元音

#include <iostream>
#include <string>
#include <sstream>

int numVowelsIncludingY(const std::string& line){
  if (line.size() == 0) return 0;
  auto is_vowel = [](char c){
    std::string vowels{"AEIOUaeiou"};
    return vowels.find(c) != std::string::npos;    
  };
  auto is_y = [](char c){ return c == 'Y' or c == 'y'; };

  size_t counter = 0;
  for (size_t i = 0; i < line.size() -1; ++i){
      auto& c = line[i];
      auto& next = line[i+1];
      if (is_vowel(c) or ( is_y(c) and !is_vowel(next) )) ++counter;
  }
  if (is_vowel(line.back()) or is_y(line.back())) ++counter;
  return counter;
}
#包括
#包括
#包括
int numowelincludingy(常量标准::字符串和行){
if(line.size()==0)返回0;
auto is_元音=[](字符c){
字符串元音{“AEIOUaeiou”};
返回元音。find(c)!=std::string::npos;
};
auto是_y=[](char c){返回c='y'或c='y';};
计数器的大小=0;
对于(size_t i=0;i


^^尝试通过调用
test(std::cin)
进行试验,并在godbolts stdin字段中键入一些输入。

下面是一个满足您的测试用例和其他几个测试用例的程序。这项工作我有两项职能。第一个检查“传统”元音。在这一点上,这样做比尝试为“Y”添加条件更容易

然后,在
count\u元音()。需要两种情况,因为第一种情况不希望超出
std::string
的范围。它可以处理单词中除最后一个位置以外的任何位置的“Y”。第二个案例检查“Y”是否是最后一个字母,并对其进行计数

#包括
#包括
#包括
//简单元音检查;“Y”的条件出现在count_元音()中
布尔是大写元音(字符c){
返回c='A'| c='E'| c='I'| c='O'| c='U';
}
int count_元音(std::string str){
整数计数=0;
用于(自动&c:str){
c=std::toupper(c);
}
对于(std::size_t i=0;istd::cout我删除了我的上一个答案,因为我没有正确理解你的问题。但现在这里的代码应该按照预期工作。是的,你不应该使用
使用命名空间std
,这被认为是一种不好的做法。 看这里:

#包括
#包括
#包括
#包括
int numowelincludingy(标准::字符串行){
int v=0;
向量向量{'a','e','i','o','u','y'};
对于(大小i=0;istd::cout Quick hack:如果在比较前将每个字符转换为大写()或小写(),则可以将所需的比较次数减半。对于
Y
逻辑,如果
i+1!=line.length()
,则选中
line[i+1]
查看它是否也是一个元音。请注意
set
,是的。代码看起来会更干净,但是#include <algorithm> #include <iostream> #include <string> #include <vector> int numVowelsIncludingY(std::string line) { int v = 0; std::vector<char> vovels{'a', 'e', 'i', 'o', 'u', 'y'}; for (size_t i = 0; i < line.length(); ++i) { char ch = tolower(line[i]); if (ch == 'y' && find(vovels.begin(), vovels.end(), tolower(line[i + 1])) != vovels.end()) { } else if (find(vovels.begin(), vovels.end(), ch) != vovels.end()) { ++v; } } return v; } int main(void) { std::cout << numVowelsIncludingY("yy"); return 0; }