到目前为止,我怎么能数出一串中有多少个辅音呢
how can i count how many consonants are in a string so far i have this
int countConsonant(string str, int consonant)
{
int length = str.length();
consonant = 0;
for (int i = 0; i < length; i++)
{
if(str[i] == 'b'&& str[i] == 'c' && str[i] == 'd'&& str[i] == 'f'
&& str[i] == 'g'&& str[i] == 'h'&& str[i] == 'j' && str[i] == 'k'&& str[i] == 'l'&& str[i] == 'm'
&& str[i] == 'n'&& str[i] == 'p'&& str[i] == 'q'&& str[i] == 'r'&& str[i] == 's'&& str[i] == 't'
&& str[i] == 'v'&& str[i] == 'w'&& str[i] == 'x'&& str[i] == 'y'&& str[i] == 'z')
consonant = consonant + 1;
}
return consonant;
}
你很接近,但应该检查逻辑或||
,而不是逻辑和&&
(str[i]
不能等于两个不同的东西)。
C++03标准允许您使用关键字and
和or
以及not
、xor
等,而不是这些神秘的(针对新程序员的)符号,但这并没有广泛流行起来——也许是因为微软的编译器在这方面没有默认为符合标准——大概是为了避免破坏现有的客户端代码,类型等。因此,为了便于携带和简化,许多图书馆和教科书也避免使用这些关键字。
另一种可能更简洁的方法是使用<cctype>
中的isalpha()
,然后检查它不是元音。更快的方法倾向于使用从字符值到bool
的数组,但要注意由于有符号字符值或>=128位非ASCII值而在数组外进行索引。如果还需要考虑大写/小写,那么在测试字符(即char c = tolower(str[i])); if (c == '...
)之前,您可能需要在字符上使用tolower()
。
其他注意事项:您的功能应该:
- 通过
const
引用(即const std::string& str
)接受其std::string
参数,以避免将值从调用上下文复制到该函数本地的单独变量中,这是不必要且耗时的。复制不会对功能造成任何真正的损害,但没有必要 - 将
consonant
设为局部变量,而不是函数参数,因为任何输入值都会立即被0替换,结果由函数返回,而不是写入consonant
(这是不可能的,因为它是通过值传递的,而不是通过指针/引用传递的)
这个怎么样:
#include <algorithm>
#include <iostream>
#include <string>
bool isVowel(char c)
{
switch (c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
{
return true;
}
default:
{
return false;
}
}
}
bool isConsonant(char c)
{
return !isVowel(c);
}
int main(int argc, char *argv[])
{
std::string test = "hello";
std::size_t numVowels = std::count_if(test.begin(), test.end(), isConsonant);
std::cout << "Number of vowels is: " << numVowels << std::endl;
return 0;
}
首先:&&
将测试所有,直到返回false
或到达终点。您想要的是||
,它将进行测试,直到条件为true
第二:什么是更短的?测试辅音还是测试元音?当然是元音。只需将测试放在一个额外的函数中(注意:我假设这里有一个有效的按字母顺序输入的字符串):
function is_vowel(c) : bool
for each vowel test
if c == that vowel
return true
return false
之后,只需将您的大条件语句替换为一个简单的!is_vowel(str[i])
.:)最后但同样重要的是,您希望递增您的consonant
变量,并且有一个特殊的运算符:递增运算符!(很酷的名字,是吗?)
++consonant; // better than consonant = consonant + 1; but same result
查看正则表达式。它允许您指定要检查的字符列表。您可以对照列表检查字符串中的每个字符,并递增计数器。
http://www.johndcook.com/cpp_regex.html
对于初学者,您使用的是&&
(和),在这种情况下这是一个逻辑错误。您要使用||
(或)。
您还可以通过检查代码是否不是元音来大大缩短代码。
int countConsonant(string str, int consonant)
{
int length = str.length();
int consonant = 0;
for (int i = 0; i < length; i++)
{
if(!(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u'))
consonant = consonant + 1;
}
return consonant;
}
其他人已经发布了该代码的另一种形式作为函数,您只需在其中传递要测试的变量。
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
size_t countConsonant(const string& s) {
const string vowels("aeiou");
size_t count = 0;
for (string::const_iterator i = s.begin(); i != s.end(); ++i) {
if (vowels.find(tolower(*i)) == string::npos) {
++count;
}
}
return count;
}
int main() {
cout << countConsonant("abcd") << endl;
}
或者,如果你不想让非阿尔法的东西匹配,你可以这样做:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
size_t countConsonant(const string& s) {
const string cons("bcdfghjklmnpqrstvwxyz");
size_t count = 0;
for (string::const_iterator i = s.begin(); i != s.end(); ++i) {
if (cons.find(tolower(*i)) != string::npos) {
++count;
}
}
return count;
}
int main() {
cout << countConsonant("abcd") << endl;
}
这篇作业太夸张了,但仅供参考-一种面向对象的方法:
struct Consonants
{
bool x_[256];
Consonants()
{
// constructor sets x_[i] to true or false to indicate
// whether character with ASCII number i is a consonant...
for (int i = 0; i < 256; ++i)
x_ = false;
// note: ASCIIZ string finishes with NUL, so *p becomes false
// and for loop exits
for (const char* p = "bcdfghjklmnpqrstvwxyz"
"BCDFGHJKLMNPQRSTVWXYZ"; *p; ++p)
x_[*p] = true;
};
int in(const std::string& str) const
{
int result = 0;
for (int i = 0; i < str.size(); ++i)
if (x_[(unsigned char)str[i]])
++result;
return result;
}
};
用法:
Consonants consonants; // create an "utility" object once
int c1 = consonants.in("hello world"); // use as often as desired
int c2 = consonants.in("goodbye cruel world");
您可以使用以下内容来计算字符串中的辅音数量:
int main()
{
char a[20];
int consonants;
gets(a);
cout<<a<<endl;
if(a[i]>=0 && a[i]<=9) // consonants
{
consonants++;
}
cout<<"Total Consonats are: "<<consonants;
}
- C++为一串单词添加空格
- 为什么Visual Studio正在清除我的一串反斜杠
- 如何将内存中的位转换为一串和零?
- 如何返回一串字母数字字符中的最大值(例如-1A003B3)?
- 创建一个程序,该程序将一串数字作为输入,并输出偶数和奇数的总和
- C++ 如何对一串数字求平均值
- 从网络数据包复制一串 wchar 时访问冲突
- C - 将文本文件读为一串单词,然后将字符串分为向量
- 我如何在一串随机生成的字母I C 中查找一个单词
- 从一串数字C++创建一个数字序列
- 将一串十六进制存储到字符中
- 在另一个字符串中查找一串数字
- 将一串数字分解为26以下数字的计数方法
- 把一串单词解析成一组单词
- 将一串数字转换为整数向量 c++
- 将一串单词视为数组 (C++)
- 使用算术记数法横向排列一串字符
- 我有编译器的情况。当我运行代码来去除一串特殊字符时,它在一个编译器中运行,但在另一个编译器中不运行?
- 到目前为止,我怎么能数出一串中有多少个辅音呢
- 接收一串"chars",并尝试使用指针计算其中有多少个相同类型