C Primer Plus(第六版)11.13 编程练习 第12题

发布时间:2024年01月14日

/*
编写一个程序,读取输入,直至读到EOF,报告读入的单词数、大写字母数、小写字母数、标点
符号数和数字字符数。使用ctype.h头文件中的函数。
*/
//测试字符串?
//ajskm,dl kdAj,.lfj sjkdl ?sdk12lfj !.,fkdj.,.lssd.1a
//(ajskm),(dl) (kdAj),.(lfj) (sjkdl) ?(sdk)12(lfj) !.,(fkdj).,.(lssd).1(a)
#include<stdio.h>
#include <ctype.h>

#define SIZE 100

int count_word(char *string);

int main(void)
{?? ?
?? ?int i,count_up=0,count_low=0,count_pun=0,count_dig=0,word=0;
?? ?char string[SIZE];
?? ?fgets(string, SIZE, stdin);
?? ?for(i=0;string[i] != '\0';i++)
?? ?{
?? ??? ?if(isupper(string[i]))
?? ??? ??? ?count_up++;
?? ??? ?else if(islower(string[i]))
?? ??? ??? ?count_low++;
?? ??? ?else if(ispunct(string[i]))
?? ??? ??? ?count_pun++;
?? ??? ?else if(isdigit(string[i]))
?? ??? ??? ?count_dig++;
?? ??? ?else continue;
?? ?}
?? ?word=count_word(string);
?? ?printf("up=%d\nlow=%d\npun=%d\ndig=%d\nword=%d\n",count_up,count_low,count_pun,count_dig,word);

?? ?return 0;
}

int count_word(char *string)
{
?? ?int i,count=0;
?? ?int in_word=0;
?? ?
?? ?for(i=0;i<SIZE;i++)?
?? ?{
?? ??? ?if(isalpha(string[i]))
?? ??? ??? ?in_word=1;
?? ??? ?else if(in_word&&isalpha(string[i])==0)
?? ??? ?{
?? ??? ??? ?count++;
?? ??? ??? ?in_word=0;?? ??? ?
?? ??? ?}
?? ??? ?else if(in_word&&string[i]=='\0')
?? ??? ?{
?? ??? ??? ?count++;
?? ??? ??? ?in_word=0;
?? ??? ?}
?? ??? ?else in_word=0;
?? ?}
?? ?return count;
}

文章来源:https://blog.csdn.net/apple_50569014/article/details/135572590
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。