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>
,然後檢查它不是元音。更快的方法傾向於使用從字符值到bool
的數組,但請注意由於有符號字符值或> = 128位非ASCII值而在數組外部建立索引。如果還要考慮大寫/小寫 - 在測試之前,您可能需要在角色上使用tolower()
(即char c = tolower(str[i])); if (c == '...
)。
其他說明:你的函數應該:
- 通過
const
引用(即const std::string& str
)接受其std::string
參數,以避免調用上下文值的不必要的耗時拷貝到一個獨立的局部變量這個功能。複製不會造成任何實際的功能損害,但是沒有必要。 - 使
consonant
是一個局部變量而不是函數參數,因爲任何輸入的值立即用0代替,並且結果由函數返回而不是寫入consonant
(這是不可能的,因爲它是按值傳遞的,而不是通過指針/引用傳遞)。
謝謝!!!!我會接受,當它允許我! – 2011-06-06 02:32:39
查看正則表達式。它允許您指定要檢查的字符列表。您將檢查字符串中的每個字符與列表並增加計數器。
我認爲在OP的作業分配中不允許使用正則表達式。 – 2011-06-06 02:30:48
我不認爲應該用正則表達式攻擊所有東西'... KISS。 – Xeo 2011-06-06 02:34:23
@xeo - 哈哈。在我看來,正則表達式更簡單=)。 @paul正則表達式對於你的hw任務來說是過分的,但它們非常棒。如果你想擴展你的編程知識,請閱讀它們! – mrtsherman 2011-06-06 02:36:14
第一:&&
,直至有返回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
對於元音部分 - 字符可能不是元音而不是輔音! – mrtsherman 2011-06-06 02:34:21
@ mrtsherman:恩,如果他們不是字母..我假設OP得到一個有效的輸入字符串,你是對的。 – Xeo 2011-06-06 02:35:12
您正在使用&&
(和),這是在這種情況下,一個邏輯錯誤首發。您想使用||
(或)。
您還可以通過檢查是否不是元音來縮短代碼。
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;
}
其他人已經發布了這種代碼的替代形式作爲一個函數,你只要傳入你想測試的變量。
關閉。但不完全相同。字符'='現在是一個常量。 – 2011-06-06 02:45:31
@Martin你是對的,我做了一些我不應該對字符串中的字符做出的假設。 – Pete 2011-06-06 02:54:09
#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;
}
第二個實現更好。或者你可以templatise計數接受一個'const char *'並實例化輔音或元音或其他需要的東西.... – 2011-06-06 03:18:20
如何:
#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;
}
isConsonant should'return!isVowel(c)&& isalpha(c);' – Marlon 2011-06-06 02:41:48
我喜歡基礎知識。但'='這個字符現在是一個常量。但不是一個開關,你可以使用一個255的數組來表示輔音的真/假。 – 2011-06-06 02:43:50
@Martin:注意,使用'signed char'可以導致負數組索引查找,因此需要先將其轉換爲'unsigned'或範圍檢查(較慢)。無論如何,'switch'相當不錯 - 編譯器應該生成相當不錯的代碼,儘管空間/速度的折衷可能不是你想要的。 – 2011-06-06 02:49:44
方式矯枉過正這個作業,但僅供參考 - 面向對象的方法:
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");
- 1. 如何計算我們的字符串有多少元音?
- 2. 我想找到在我的字符串中有多少元音和輔音C
- 3. Python:計算字符串中有多少個字母
- 4. 字母至少有1字
- 5. 計算字符串中有多少個特定字符
- 6. 如何計算給定字符串中有多少個「ZE」
- 7. 字符串中每個字母至少有一次?
- 8. 如何計算Ruby中字符串開頭的連續輔音?
- 9. 我如何讓Python計算一個單詞中有多少個字母?
- 10. 計算兩個字符串有多少個重複字符
- 11. 計算字符串中的元音和輔音的數量
- 12. 計數元音和輔音字符串
- 13. R:計算從字符串開始匹配多少字母
- 14. 怎麼算印地文字符串數組與PHP和統計有多少字母和元音串
- 15. 如何計算字符串中的字母字符?
- 16. bash字符串至今
- 17. 解碼字符串至今
- 18. Python字符串至今
- 19. 字符串中有多少個不同的字母
- 20. 計算字符串中所有字母的出現次數PHP
- 21. 如何計算UILabel行中有多少個字符?
- 22. 如何計算在PL/SQL中有多少單詞有給定的字母
- 23. Golang中如何計算字節中有多少位字節?
- 24. 我如何計算字符串中特定字符的數量?
- 25. 在包含Y的字符串中計算元音,如果其後跟輔音
- 26. 如何計算具有「字符串」值
- 27. 字符串名稱;方法1:至少2個字,最多4個字,沒有特殊標誌,只有字母
- 28. 計算多少個字符
- 29. 如何查找字符串上有多少個數字和字母
- 30. 計數列表中有多少個字,字符串中使用
大聲笑,你的代碼基本上說,如果str [i]是所有的字母一次的輔音。這是一個超級字符! – Marlon 2011-06-06 02:32:42