在过去的两天里,我一直在到处寻找,但无法弄清楚。这是输出的一个小样本:
它应该是“并找到乐趣”:
和
F
工业
一个
p
休闲
=================
长度计数
=================
1 9054
2 10102
3 9336
4 5944
5 3311
6 1656
7 1292
=================
平均 2.86
=================
下面是代码:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define DELIM " ,.;:'\"&!? -_\n\t\0"
int process(int *count, char *buffer);
int printOut(int *count);
int main() {
char *buffer = (char *) calloc(1, 50*sizeof(char*));
int *count =(int*) calloc(50, 50*sizeof(long));
while (fgets(buffer, sizeof buffer, stdin)) {
process(count, buffer);
}
printOut(count);
free(count);
return 0;
}
int process(int *count, char *buffer) {
int word_len=0, i;
char *pch;
pch = strtok(buffer, DELIM);
while (pch != NULL) {
for(i=0; pch[i] != '\0'; i++) {
word_len++;
}
count[word_len]++;
word_len=0;
printf("%s\n", pch);
pch = strtok(NULL, DELIM);
}
return 0;
}
int printOut(int *count) {
int i;
double num=0;
double total=0;
double average=0;
printf("================\n");
printf("len count\n");
printf("================\n");
for(i=0;i<50;i++){
if(count[i]!=0){
num=count[i]+num;
total=total+(count[i]*i);
printf("%d %d\n",i,count[i]);
}
}
average = total/num;
printf("================\n");
printf("average %.2f\n", average);
printf("================\n");
return 0;
}