如何知道一个角色是否是一个元音。 [英] how to know if a character is a vowel.

查看:75
本文介绍了如何知道一个角色是否是一个元音。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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

推荐答案

您正在尝试将数组与单个字符进行比较:不可能。您需要使用类似的内容搜索每个字符的数组 string <中找到 / code> class [ ^ ]。
You are trying to compare an array to a single character: impossible. You need to search the array for each character, using something like find in the string class[^].


你需要使用==运算符才能使这种方法有效。(你可以重载它)它需要比较数组中每个字符的单个字符。为了兼顾可维护性和可重用性,你应该写一个函数,它将需要



(1)要检查的字符

(2)一个以null结尾的字符串,其中包含要检查的字符



如果(1)匹配(2)中的任何字符,则返回true - strtok使用方法模糊地相似 - 也就是说,使用以空字符结尾的字符串来传递真正的其他无关字符数组。



另一种方法你可以做它将使用switch语句。由于多个案例可以有相同的动作,你可以用这种天真的方式将它们全部串在一起,尽管外观很尴尬,但实际上也很快。



You'd need the == operator to work differently in order for this approach to work. (You can overload it) It would need to compare the single character to each of the characters in the array. In the interests of both maintainability and re-usability, you should really write a function that will take

(1) the character to check
(2) a null-terminated string that contains characters to check against

Returning true if (1) matches any of the chars in (2) - strtok employs an approach vaguely similar - that is, one of using a null-terminated string to pass what is really an array of otherwise-unrelated characters.

An other way you could do it would be to use a switch statement. Since multiple cases can have the same action, you could just string them all together in this naive manner, which would also actually be pretty fast, despite its awkward appearance.

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;
}


您必须将 sSentence [o] 与<$的每个项目进行比较c $ c> cVowels 数组(或者,至少,直到找到匹配项),例如

You must compare sSentence[o] with every item of the cVowels array (or, at least, until a match is found), e.g.
change from
引用:

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
}


这篇关于如何知道一个角色是否是一个元音。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆