计算字符串中辅音的个数
Count number of consonants in a string
另一个入门问题。
int counterConstant;
int x;
for(x = 0; x<20; x++){
if("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSVWXYZ".IndexOf(tempString[x]) >= 0){
counterConsonant++;
}
}
但是我得到一个错误:
"error: member reference base type 'char [42]' is not a structure or union"
还有其他方法吗?
(我在 for
中执行此操作,检查 string
上的每个 char
。)
char temp[20];
scanf("%s",temp);
int i,j, consonantsCounter=0;
char consonants[]={'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z','B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','V','W','X','Y','Z'}
for(i=0;i<20;i++){
for(j=0;j<(sizeof consonants) / (sizeof consonants[0]);j++){
if(temp[i]==consonants[j]){
consonantsCounter++;
}
}
}
如果您正在使用 C(在标签中指定),strchr()
方法用于搜索字符串中的字符,strstr()
用于搜索字符串中的字符串细绳。我们将在这里使用 strchr()
因为 tempString[x]
是一个字符。另外,不要忘记给 int
变量一个初始值。试试这个代码:
int main()
{
int counterConsonant = 0;
int x;
const char* tempString = "12345678901234567890";
for (x = 0; x<20; x++){
if (strchr("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSVWXYZ", tempString[x]) != NULL){
counterConsonant++;
}
}
return 0;
}
C 中没有对象,所以没有 "methods" 并且您不能在字符串文字上调用 IndexOf
。字符串无非是C中的一个字符数组。
考虑到这一点,让我们看看如何实际遍历字符串的字符:
for (const char *p = tempString; *p != '[=10=]'; ++p) {
/* loop body */
char c = *p; // *p is the current letter
}
这将创建一个指向字符串第一个元素的指针,然后循环遍历以下所有字符,如果你真的更喜欢使用索引,你可以这样做
for (size_t i = 0, len = strlen(tempString); i < len; ++i) {
char c = tempString[i];
}
就检查每个字母的辅音而言,您可以为
编写一个辅助函数
int is_consonant(char c) {
c = tolower(c); // #include <ctype.h>
if (!isalpha(c)) return 0; // if not a letter, return false
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return 0;
default:
return 1;
}
}
现在回到你的循环,使用这个函数来检查每个字符。
int consonant_count = 0; // the =0 is important!
for (const char *p = tempString; *p != '[=13=]'; ++p) {
if (is_consonant(*p)) {
++consonant_count;
}
}
如果你不初始化为0,consonant_count
的初始值是不可预测的,所以一定要这样做。
C 是一种结构化过程语言,因此它不像 "true" 面向对象的编程语言(例如 C#)那样具有成员 functions/methods。您可以使用 strspn
和 strcspn
的组合,如下所示,根据预定义的辅音字符列表分别计算辅音和非辅音字符的序列:
#include <string.h>
size_t
count_consonants (const char *s)
{
size_t n;
size_t total = 0;
const char *consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
/* While we haven't reached the end of the string,
execute the code in the body of the loop. */
while (*s != '[=10=]')
{
/* Count the number of consonants starting at the current string position. */
n = strspn (s, consonants);
/* Add the number of consonants counted to
the total number of consonants found. */
total += n;
/* Advance the character pointer to the next character
that IS NOT a consonant, based on the number of consonants
stored in `n'. */
s += n;
/* Advance the character pointer to the next character
that IS a consonant (`strcspn' = skip the characters in
`s' that don't appear in `consonants'). */
s += strcspn (s, consonants);
}
return total;
}
另一个入门问题。
int counterConstant;
int x;
for(x = 0; x<20; x++){
if("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSVWXYZ".IndexOf(tempString[x]) >= 0){
counterConsonant++;
}
}
但是我得到一个错误:
"error: member reference base type 'char [42]' is not a structure or union"
还有其他方法吗?
(我在 for
中执行此操作,检查 string
上的每个 char
。)
char temp[20];
scanf("%s",temp);
int i,j, consonantsCounter=0;
char consonants[]={'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z','B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','V','W','X','Y','Z'}
for(i=0;i<20;i++){
for(j=0;j<(sizeof consonants) / (sizeof consonants[0]);j++){
if(temp[i]==consonants[j]){
consonantsCounter++;
}
}
}
如果您正在使用 C(在标签中指定),strchr()
方法用于搜索字符串中的字符,strstr()
用于搜索字符串中的字符串细绳。我们将在这里使用 strchr()
因为 tempString[x]
是一个字符。另外,不要忘记给 int
变量一个初始值。试试这个代码:
int main()
{
int counterConsonant = 0;
int x;
const char* tempString = "12345678901234567890";
for (x = 0; x<20; x++){
if (strchr("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSVWXYZ", tempString[x]) != NULL){
counterConsonant++;
}
}
return 0;
}
C 中没有对象,所以没有 "methods" 并且您不能在字符串文字上调用 IndexOf
。字符串无非是C中的一个字符数组。
考虑到这一点,让我们看看如何实际遍历字符串的字符:
for (const char *p = tempString; *p != '[=10=]'; ++p) {
/* loop body */
char c = *p; // *p is the current letter
}
这将创建一个指向字符串第一个元素的指针,然后循环遍历以下所有字符,如果你真的更喜欢使用索引,你可以这样做
for (size_t i = 0, len = strlen(tempString); i < len; ++i) {
char c = tempString[i];
}
就检查每个字母的辅音而言,您可以为
编写一个辅助函数int is_consonant(char c) {
c = tolower(c); // #include <ctype.h>
if (!isalpha(c)) return 0; // if not a letter, return false
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return 0;
default:
return 1;
}
}
现在回到你的循环,使用这个函数来检查每个字符。
int consonant_count = 0; // the =0 is important!
for (const char *p = tempString; *p != '[=13=]'; ++p) {
if (is_consonant(*p)) {
++consonant_count;
}
}
如果你不初始化为0,consonant_count
的初始值是不可预测的,所以一定要这样做。
C 是一种结构化过程语言,因此它不像 "true" 面向对象的编程语言(例如 C#)那样具有成员 functions/methods。您可以使用 strspn
和 strcspn
的组合,如下所示,根据预定义的辅音字符列表分别计算辅音和非辅音字符的序列:
#include <string.h>
size_t
count_consonants (const char *s)
{
size_t n;
size_t total = 0;
const char *consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
/* While we haven't reached the end of the string,
execute the code in the body of the loop. */
while (*s != '[=10=]')
{
/* Count the number of consonants starting at the current string position. */
n = strspn (s, consonants);
/* Add the number of consonants counted to
the total number of consonants found. */
total += n;
/* Advance the character pointer to the next character
that IS NOT a consonant, based on the number of consonants
stored in `n'. */
s += n;
/* Advance the character pointer to the next character
that IS a consonant (`strcspn' = skip the characters in
`s' that don't appear in `consonants'). */
s += strcspn (s, consonants);
}
return total;
}