不同单词个数统计

样例输入

one little two little three little boys

样例输出

5

学会将一个字符串分为多个字符串比较。
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 char a[10100];
 5 int f(){
 6     int i, j, k, n;
 7     char temp[30];
 8     char s[100][30];
 9     
10     j = 0, k = 0;
11     for(i = 0; a[i] != '\0'; i++){
12         if(a[i] != ' '){
13             temp[j++] = a[i];
14         }
15         if(a[i] != ' ' && (a[i+1] == ' ' || a[i+1] == '\0')){
16             temp[j] = '\0';
17             j = 0;
18             if(k == 0){
19                 strcpy(s[k++],temp);
20             }else{
21                 for(n = 0; n < k; n++){
22                     if(strcmp(s[n],temp) == 0)break;
23                 }
24                 if(n == k){
25                     strcpy(s[k++],temp);
26                 }
27             }
28         }
29     }
30     return k;
31 } 
32 int main() {
33 
34     gets(a);
35     printf("%d",f());
36     
37     return 0;
38 }

 

 
posted @ 2021-01-27 14:48  荣荣荣荣荣荣  阅读(99)  评论(0)    收藏  举报