|
|
发表于 2012-8-21 14:24:58
|
显示全部楼层
本帖最后由 the_one 于 2012-8-21 17:58 编辑
这个应该可以- #include <stdio.h>
- #define MAX_LEN 100
- void count(char *s,int *num,int *blank,int *others)
- {
- for (int i=0 ; i<MAX_LEN && s[i]!='\0' ; i++)
- {
- if (s[i]>='0' && s[i]<='9')
- num[ s[i]-48 ]++;
- else if(s[i]==' ' || s[i]==9 || s[i]=='\n')//9可以换成''里面按一下Tab键,论坛里发不出来
- (*blank)++;
- else
- (*others)++;
- }
- }
- int main()
- {
- char s[MAX_LEN] = "test string 1";
- int num[10],blank=0,others=0;
- for (int i=0 ; i<10 ; i++)
- num[i] = 0;
- count(s,num,&blank,&others);
- for (int j=0 ; j<10 ; j++)
- printf("%d:%d\n",j,num[j]);
- printf("空白符:%d\n其他字符:%d\n",blank,others);
- return 0;
- }
复制代码 如果想不用指针的话,可以声明为全局变量 |
|