gpt4 book ai didi

c++ - isspace 和 isalpha 不算数

转载 作者:行者123 更新时间:2023-11-30 01:45:45 25 4
gpt4 key购买 nike

我的程序应该计算输入字符串中元音、辅音、数字和空格的数量。它只计算位数。请帮忙。

#include<iostream>
using namespace std;

void counter(char string[], int count[]){
int check_letter, check_digit, check_space;
for(int i = 0; i < 99; i++){
check_letter = isalpha(string[i]);
if(check_letter == 1){
string[i] = tolower(string[i]);
if(string[i] == 'a' || string[i] == 'e' || string[i] == 'i' ||
string[i] == 'o' || string[i] == 'u'){
count[0] = count[0] + 1;
} else{
count[1] = count[1] + 1;
}
}
check_digit = isdigit(string[i]);
if (check_digit == 1){
count[2] = count[2] + 1;
}
check_space = isspace(string[i]);
if(check_space == 1){
count[3] = count[3] + 1;
}
}
}

main(){
char string[100] = {};
int count[4] = {};
cout << "Please enter a string: ";
cin.get(string, 100);
cin.get();
cout << string;
counter(string, count);
cout << "There are " << count[0] << " vowels.\nThere are " << count[1] <<
" consonants.\nThere are " << count[2] << " digits.\nThere are " <<
count[3] << " spaces.";
}

最佳答案

您的问题是假设 isalphaisdigit 等在匹配时返回 1:他们的文档说他们返回非零“true”的值和“false”的零值。

例如,来自std::isalpha docs here :

Return value

Non-zero value if the character is an alphabetic character, zero otherwise.

如果您将结果存储在 bool 中,或者直接在 bool 上下文中测试它们(例如 if (...) 条件,那么转换将是为您完成。

关于c++ - isspace 和 isalpha 不算数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com