一道题_20121216

来源:互联网 发布:java static volatile 编辑:程序博客网 时间:2024/06/06 07:10

开始写写博客,做做笔记,学习学习。

输入一段英文,统计其中每个英文单词出现的次数,并输出,输出形式如下:

输入 the number is the number
结果: the   number   is
            2      2             1


#include <stdio.h>#include <string.h>int main(){char ch = '\0';char str[100] = {'\0'};int i = 0;printf("输入一段英文:");while((ch = getchar()) != '\n'){str[i] = ch;i ++;}char *str1[20] = {'\0'};i = 0;str1[0] = strtok(str, " ");while(str1[i] != NULL){i++;str1[i] = strtok(NULL, " ");}int szCalaute[100] = {0};int temp = 0, it = 0;for(int m = 0; m < i; m++){int k =1;for(int n = 0; n < i; n++){if((strcmp(str1[m], str1[n]) == 0) && m != n){//确定第1个与之相同单词的位置,用temp保存if(k == 1) temp = n;k++;}}//相同单词出现次数大于2的情况if(k >= 2 && m > temp) continue;printf("%s\t", str1[m]);szCalaute[it] = k;it++;}printf("\n");temp = 0;while(temp != it){printf("%d\t", szCalaute[temp]);temp++;}printf("\n");return 0;}

运行结果如下:



原创粉丝点击