本文介绍了伯爵没有元音的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序来计算无元音的一个字符串,但它的效率不高或优化code。此外,它不会检查瓶盖元音。

 #包括< iostream.h时>
使用名字空间std;
诠释的main()
{
无符号整型vow_cnt = 0;
字符名称[15] =sijith AEU;
COUT<<输入名称<< ENDL;
CIN>>名称;
为(unsigned int类型I = 0; I< strlen的(名字),我++)
{
  如果(名称[I] =='A'||名[我] =='E'||名[我] =='我'||名[我] =='O'||名[I] =='U')
  {
   vow_cnt ++;
  }
 }
COUT<<vow_cnt<< vow_cnt<< ENDL;
}
 

解决方案

考虑到你的假设,即只有AEIOU是元音和你的字符串是全部小写,试试这个:当然,这要在UNI code或具有不同的元音每一种语言可怕的失败。

 布尔is_vowel(字符X){
  //为了通过发生在目标语言概率
  // 例如。开始随e英语
  // NB看到进一步的细节评论
  则返回(x =='E'|| ...);
}

标准::字符串FOO;
长nbVowels =的std :: count_if(foo.begin(),foo.end(),is_vowel);
 

I wrote a program to count no of vowels in a string but its not efficient or optimized code.Moreover it will not check caps vowels.

#include<iostream.h>
using namespace std;
int main()
{
unsigned int vow_cnt=0;
char name[15]= "sijith aeu";
cout<<"Enter a name"<<endl;
cin>>name;
for(unsigned int i=0;i<strlen(name);i++)
{ 
  if(name[i] == 'a' || name[i] == 'e'||name[i] == 'i'||name[i] == 'o'||name[i] == 'u')
  { 
   vow_cnt++;
  }
 }
cout<<"vow_cnt"<< vow_cnt << endl;
}
解决方案

Considering your assumption that only a e i o u are vowels and that your string is all lowercase, try this:Of course this going to fail horribly in unicode or for every language that has a different set of vowels.

bool is_vowel(char x) {
  // order by probability of occurrence in the target language
  // e.g. start with e for English
  // nb see comments for further details
  return (x == 'e' || ...); 
}

std::string foo;
long nbVowels = std::count_if(foo.begin(), foo.end(), is_vowel);

这篇关于伯爵没有元音的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:12