'commit'
This commit is contained in:
42
C++专题课程/基础知识/统计个数(数字、字符、其它字符).cpp
Normal file
42
C++专题课程/基础知识/统计个数(数字、字符、其它字符).cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#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); //<2F><><EFBFBD><EFBFBD>'\n'<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user