C++ 如何确定字符是否为元音
我正在尝试使用C++ 如何确定字符是否为元音,c++,C++,我正在尝试使用vector[].substr(),但我不知道这是否可行。有人知道另一种方法吗?我的目标是把向量中的单词和第一个元音分开。感谢您的帮助。我的代码如下所示: #include <iostream> #include "derrick_math.h" #include <string> #include <sstream> #include <vector> #include <cctype> #include <alg
vector[].substr()
,但我不知道这是否可行。有人知道另一种方法吗?我的目标是把向量中的单词和第一个元音分开。感谢您的帮助。我的代码如下所示:
#include <iostream>
#include "derrick_math.h"
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;
int main()
{
string phrase;
string ay = "ay";
vector<string> vec;
cout << "Please enter the word or phrase to translate: ";
getline(cin, phrase);
istringstream iss(phrase);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter(vec));
for (int i = 0; i < vec.size(); i++)
{
if (vec[i].substr(0, 1) == "a || e || i || o || u || A || E || I || O || U")
{
cout << vec[i] << "ay";
}
if (vec[i].substr(1, 1) == "a || e || i || o || u || A || E || I || O || U")
{
cout << vec[i].substr(2) << vec[i].substr(0, 1) << "ay";
}
if (vec[i].substr(2, 1) == "a || e || i || o || u || A || E || I || O || U")
{
cout << vec[i].substr(3), vec[i].substr(0, 2) + "ay";
}
if (vec[i].substr(3, 1) == "a || e || i || o || u || A || E || I || O || U")
{
cout << vec[i].substr(4), vec[i].substr(0, 3) + ay;
}
cout << vec[i] << " ";
}
cout << endl;
system("pause");
return 0;
#包括
#包括“derrick_math.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串短语;
字符串ay=“ay”;
向量向量机;
cout访问向量元素的成员函数不是您的问题。您的if语句格式不正确。当前您正在将子字符串与一个长字符串进行比较,在这种情况下,该值永远不会为true
如果要检查特定字符,则需要以下内容:
bool is_vowel(char c) {
c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
或者
现在可以这样调用函数:
std::string s = vec[i];
if(is_vowel(s[n])) {
// the third character is a vowel
}
您的代码还存在一些其他问题。这一行:
cout << vec[i].substr(4), vec[i].substr(0, 3) + ay;
访问向量元素的成员函数不是您的问题。您的if语句格式不正确。当前您正在将子字符串与一个长字符串进行比较,在这种情况下,该值永远不会为true
如果要检查特定字符,则需要以下内容:
bool is_vowel(char c) {
c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
或者
现在可以这样调用函数:
std::string s = vec[i];
if(is_vowel(s[n])) {
// the third character is a vowel
}
您的代码还存在一些其他问题。这一行:
cout << vec[i].substr(4), vec[i].substr(0, 3) + ay;
你可以用
这将为参数中建立的任何字符找到第一个匹配项的位置。
然后用substr将其分开,就像
for (int i = 0; i < vec.size(); ++i){
size_t pos = vec[i].find_first_of("aAeEiIoOuU");
std::string result = "";
if (pos != std::string::npos){ // Check if find_first_of got a match
result = vec[i].substr(pos + 1);
}
std::cout << "result = " << result << std::endl;
}
for(int i=0;i
这将为参数中建立的任何字符找到第一个匹配项的位置。
然后用substr将其分开,就像
for (int i = 0; i < vec.size(); ++i){
size_t pos = vec[i].find_first_of("aAeEiIoOuU");
std::string result = "";
if (pos != std::string::npos){ // Check if find_first_of got a match
result = vec[i].substr(pos + 1);
}
std::cout << "result = " << result << std::endl;
}
for(int i=0;i std::cout为了你自己的缘故,写一些像bool-isvoral(char)
。这将对你有很大帮助。如果vec
是std::vector
,你希望bool-isvoral(char)做什么呢
。这将给你极大的帮助。如果vec
是一个std::vector
,你希望vec[].substr()
做什么?这会让事情变得更简单:s.find\u first\u of(“AEIOUaeiou”)
@Zeta:当然,但我想说明他的if语句有什么问题。我也会做同样的事情,请看。尽管如此,OP仍然想找到第一个元音,您的示例使用了if(is_votel(s[n])
(我想您的意思是s[2]
btw).那为什么就到此为止呢?这是一个介绍for
或std::find
或参考find\u first\u of
@Zeta:我的意思是s[n]
。他检查了多个索引;我只是展示了索引到一个字符串中会返回一个字符
。我可以添加更多的方法来解决这个问题,但老实说,对于一个还没有掌握语法的人,我不相信深入了解更多细节是非常有帮助的。这当然不会有什么坏处,但他仍然有办法l、 会让事情变得更简单:s.find_first_of(“AEIOUaeiou”)
@Zeta:当然,但我想展示一下他的if语句有什么问题。我也会做同样的事情,看。不过OP仍然想找到第一个元音,你的例子使用了if(is_元音(s[n])
(我想你的意思是s[2]
btw).那为什么就到此为止呢?这是一个介绍for
或std::find
或参考find\u first\u of
@Zeta:我的意思是s[n]
。他检查了多个索引;我只是展示了索引到一个字符串中会返回一个字符
。我可以添加更多的方法来解决这个问题,但老实说,对于一个还没有掌握语法的人,我不相信深入了解更多细节是非常有帮助的。这当然不会有什么坏处,但他仍然有办法L