1178: word count

1178: word count

Topic Description
Count the total number of distinct words in an article.
Input
There are multiple sets of data, each set is a row, and each set is a small article. Each small article is composed of uppercase and lowercase letters and spaces, without punctuation marks, and the end of input when # is encountered. The number of words per article is less than 1000, and each word consists of up to 30 letters.
Output
Each group outputs only one integer, on its own line, that represents the total number of distinct words in an article.
Sample input Copy
you are my friend

Sample Output Copy
4
Source/Classification

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100

int main(){
    
    
    char s[30000];
    char *p[1000];


    while(gets(s),strcmp(s,"#")!=0){
    
    
        int j=0;//统计单词的个数
        int k=0;//每个单词的字符
        int count=0;//不重复的单词个数
        
        for(int i=0;i<strlen(s);i++){
    
    
            if(s[i]!=' '){
    
    
                k=0;
                p[j]=(char *)malloc(sizeof(char)*30);

                for(i;s[i]!=' '&&i<strlen(s);i++){
    
    
                    p[j][k++]=s[i];
                }
                p[j][k]='\0';

                j++;
            }
        }

        //第i个单词的后面没有与它相同的就+1,结果需要再+最后一个单词
        int f=1;//表示某一单词的后面没有与之重复的
        for(int i=0;i<j-1;i++){
    
    
            for(int k=i+1;k<j;k++){
    
    
                    f=1;
                    if(strcmp(p[i],p[k])==0){
    
    
                        f=0;
                        break;
                    }
            }
            if(f==1) count++;
        }
        printf("%d\n",count+1);

    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44500344/article/details/108127730