acm c语言训练 数不同的单词数

题目: WUST 零起点学算法92——单词数
Description
BobLee 最近忙着考研,话说某一天当他正在看一篇英语阅读时,突然想到想去统计下这篇文章不同单词的个数,由于BobLee很忙,所以想让你帮忙统计一下
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。(保证每行不超过1000个字符)
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数
Sample Input
you are very kind
#
Sample Output
4
测试代码一:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int fun_rem(char *s,char rem[200][100]);
int main()
{
    char *s = (char *)calloc(1001, sizeof(char));
    char rem[200][100] = {0,}; // to remeber the diffrent word
        while (gets(s), strcmp(s, "#") != 0) // if s="#" ends the progress
        {
            printf("%d\n", fun_rem(s, rem));
        }
    return 0;
}
int fun_rem(char *s,char rem[200][100])
{
    unsigned int flag = 1;
    int i, j, k, lenth = strlen(s), cnt = 0,count=0;
    char s1[40] = { 0 ,};
    for (i = 0,j=0; i < lenth; i++)// to count the different words
    {
        if (*(s + i) != ' ') *(s1 + j++) = *(s + i);  // put the word into string s1
        if(i==lenth-1 || (*(s + i) == ' ' &&  *(s + i - 1) != ' ')) // memory the words in rem
        {
            *(s1 + j) = 0;//set an end to s1
            j = 0; //reset the varity j
            for (k = 0; k < cnt; k++)// to know if the new s1 is diiferent of all the words in rem
                if (strcmp(s1,*( rem + k)) == 0) flag = 0;
            if (flag == 1) { strcpy(rem[cnt++], s1); count++; }
            flag = 1;//reset
        }
    }
    return count;
}

结果:
这里写图片描述
o j: runtime error

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值