作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序应该计算输入字符串中元音、辅音、数字和空格的数量。它只计算位数。请帮忙。
#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.";
}
最佳答案
您的问题是假设 isalpha
、isdigit
等在匹配时返回 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/
我是一名优秀的程序员,十分优秀!