You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
python/C++专题课程/基础知识/统计个数(数字、字符、其它字符).cpp

43 lines
785 B

#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;
}