我的程序應該計算輸入字符串中的元音,輔音,數字和空格的數量。它只計算數字的數量。請幫忙。isspace和isalpha不計數
#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.";
}
是的,你可以,但'int check_letter,check_digit,check_space;'是沒用的...... –