本文介绍了如何知道一个角色是否是一个元音。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
string sSentence;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,sSentence) )
{
cout << sSentence << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
int iLen = sSentence.length();
int iBlank = sSentence.find(" ");
char cVowels[]={"AEIOUaeiou"};
for(int o=0;0<iLen;o++){
if(isalpha(sSentence[o]) && sSentence[o] == cVowels ){
string sSen = sSentence.substr(o,iBlank);
int iLength = sSen.length();
int iVowel = sSen.find_first_of("aeiouAEIOU");
string addSent = sSen.substr(0,iVowel);
string newSent = sSen.insert(iLength,addSent);
cout << newSent;
cout<<"ay";
o=iBlank-1;
}
else
cout << sSentence[o];
}
return 0;
}
i有此条件
i have this condition
char cVowels[]={"AEIOUaeiou"};
if(isalpha(sSentence[o]) && sSentence[0] == cVowels )
条件表明该字符是一个字母,它必须是一个元音。
如何将字符与cVowels进行比较???
当我运行代码时说:
[错误]预期主要 - 在']'之前的表达式令牌
the condition states that the character is a letter and it must be a vowel.
how can I compare the character to cVowels???
when I run the code it says:
"[error] expected primary - expression before ']' token
推荐答案
switch (sSentence[index])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
// character is a vowel
// do something here
break;
default:
// not a vowel
// do something else here
}
编辑:添加方法显示使用一个函数。
Added approach showing the use of a function.
#include <cstdlib>
#include <cstdio>
#include <cstring>
bool isCharOneOf(char lookForMe, const char *inHere)
{
while (*inHere)
{
if (lookForMe == *inHere)
return true;
inHere++;
}
return false;
}
int main(int argc, char *argv[])
{
const char *searchString = "The quick brown fox jumps over the lazy dog";
const char *vowels = "aeiouAEIOU";
int nChars, curIndex, nVowels=0;
nChars = strlen(searchString);
for (curIndex=0; curIndex<nChars; curIndex++)
{
if (isCharOneOf(searchString[curIndex], vowels) == true)
nVowels++;
}
printf("There are %d vowels in '%s'\n", nVowels, searchString);
return 0;
}
引用:
if(isalpha(sSentence [o])&& sSentence [o] == cVowels)
if(isalpha(sSentence[o]) && sSentence[o] == cVowels )
to
to
int n;
for (n=0; n<sizeof(cvowels); n++) {
if ( sSentence[o] == cVowels[n])
{ // OK, it is a vowel, do stuff here
//..
break;
}
}
if ( n == sizeof(cvowels))
{ // no match: it is not a vowel
}
这篇关于如何知道一个角色是否是一个元音。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!