文章

36

粉丝

0

获赞

4

访问

10.1k

头像
统计单词 题解:c 注意分类讨论
P1394 华中科技大学
发布于2024年3月6日 13:42
阅读数 136

#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

int main(void) {
    char s[1000];
    int count;
    while (gets(s)!=NULL) {
        for(int i=0;i<strlen(s);i++){
            if(s[i]=='.'&&(s[i-1]>='a'&&s[i-1]<='z')){          //如果识别到‘.’并且‘.’前一位是字母才会打印 推出循环
                printf("%d ",count);
                break;
            }
            else if(s[i]!=' '){             //如果识别字符不是空格
                if(s[i-1]==' '||i==0) count = 1;    //如果前一位是空格或者是第一个单词,则开始计数
                else if(s[i-1]!=' ' && i!=0) count++; //如果前一位不是空格,就继续计数
            }
            else if(s[i]==' '&&(s[i-1]>='a'&&s[i-1]<='z')) printf("%d ",count); //如果是单词后第一个空格,输出
        }
        printf("\n");
    }
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发