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

/*
编写一个程序,读取输入,直至读到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;
}

相关推荐

  1. C Primer Plus(11.13 编程练习 12

    2024-01-18 12:50:02       61 阅读
  2. C Primer Plus(13.11 编程练习 12

    2024-01-18 12:50:02       45 阅读
  3. C Primer Plus(13.11 编程练习 11

    2024-01-18 12:50:02       48 阅读
  4. C Primer Plus(13.11 编程练习 13

    2024-01-18 12:50:02       45 阅读
  5. C Primer Plus(12.9 编程练习 5

    2024-01-18 12:50:02       46 阅读
  6. C Primer Plus(11.13 编程练习 6

    2024-01-18 12:50:02       44 阅读
  7. C Primer Plus(12.9 编程练习 2

    2024-01-18 12:50:02       47 阅读
  8. C Primer Plus(13.11 编程练习 1

    2024-01-18 12:50:02       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 12:50:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 12:50:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 12:50:02       82 阅读
  4. Python语言-面向对象

    2024-01-18 12:50:02       91 阅读

热门阅读

  1. 计算机网络、浏览器面试题

    2024-01-18 12:50:02       43 阅读
  2. 如何解决redis热点key问题

    2024-01-18 12:50:02       52 阅读
  3. Go自研微服务框架-日志处理

    2024-01-18 12:50:02       43 阅读
  4. Android设置夜间模式的主题样式

    2024-01-18 12:50:02       49 阅读
  5. .Net CSRF 跨站点请求伪造漏洞

    2024-01-18 12:50:02       50 阅读
  6. QT基础篇(9)QT5文件及磁盘处理

    2024-01-18 12:50:02       66 阅读
  7. 6.【CPP】Date类的实现

    2024-01-18 12:50:02       46 阅读
  8. 用Python实现给图片去黑边

    2024-01-18 12:50:02       47 阅读