// count digital whitespace and others
#include <stdio.h>
int main()
{
// initalize the data
int c, i, nwhite, nothers;
int digital[10];
nwhite = nothers = 0;
for (i = 0; i <= 9; ++i)
{
digital[i] = 0;
}
// read and count
while ((c = getchar()) != EOF)
{
if (c >= '0' && c <= '9')
++digital[c - '0'];
else if (c == ' ' || c == '\t' || c == '\n')
++nwhite;
else
++nothers;
}
//output the result
printf("digital = \n");
for (i = 0; i <= 9; ++i)
printf("the times of %d is %d\n", digital[i],digital[i]);
printf("\n,nwhite is %d, nothers is %d", nwhite, nothers);
}
//学会读取字符,且能将其转化成数字
看的书上一步步做的,有以下几点新手需要注意的:
1,\n也算一个字符,按enter键也算。
2,for循环里用;隔开
3,数组的索引是从0到9的,int digital[10];
nwhite = nothers = 0;
for (i = 0; i
这里10应该改为9
4,getchar函数读取字符时要转换处理,读取的是字符
也是希望有大佬指导,等以后开始做比较大的项目时,就开始往GitHub上传。
作者我同时还在学python,感觉对python更感兴趣,python以它的简洁,高效深深吸引了我。
惯例附上张鼓励的图片