Files
python/C++专题课程/基础知识/统计个数(数字、字符、其它字符).cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
827 B
C++

#include<bits/stdc++.h>
using namespace std;
#define MAX 1024
void cal_num(char *str, int count[]) {
char *pstr;
pstr=str;
while(*pstr) { /* *pstr != 0 */
if(*pstr>='0' && *pstr<='9')
count[0]++;
else if((*pstr>='a' && *pstr<='z') || (*pstr>='A' && *pstr<='Z'))
count[1]++;
else
count[2]++;
pstr++;
}
}
int main() {
char str[MAX];
int i, count[3]; /* 0->num; 1->char; 2->others */
memset(count, 0, 3*sizeof(int));
printf("Enter a string: ");
//scanf("%s", str);
scanf("%[^\n]",str); //¶Áµ½'\n'½áÊø¶ÁÈ¡
cal_num(str, count);
for(i=0; i<3; i++) {
switch(i) {
case 0:
printf("num: %d\n", count[i]);
break;
case 1:
printf("char: %d\n", count[i]);
break;
case 2:
printf("other: %d\n", count[i]);
break;
}
}
return 0;
}