zoukankan      html  css  js  c++  java
  • 程序2:word count

    本程序改变自:http://blog.csdn.net/zhixi1050/article/details/72718638

    语言:C++

    编译环境:visual studio 2015

    运行环境:Win10

    做出修改的地方:在原码基础上修改了记录行数的功能,删去了不完整行数的记录,直接显示行数。

    修改后的代码如下:

    #include <stdio.h>
    //#include <stdlib.h>
    #include <ctype.h>//为isspace()提供原型
    #include <stdbool.h>
    #define STOP '|' //定义结束标志


    int main(void)
    {
        char c;
        char prev;//读取的前一个字符
        long n_chars = 0L;      //字符数
        int n_lines = 0;            //行数
        int n_words = 0;          //单词数
        bool inword = false;      //字符在单词中,inward等于ture

        printf("请输入字符( | 用于结束输入): ");
        prev = ' ';//识别完整的行

        while ((c = getchar()) != STOP)      //当读取的字符不为结束字符时
        {
            n_chars++      ;//统计字符数

            if (c == ' ')
                n_lines++;      //统计行

            if (!isspace(c) && !inword)
            {
                inword = true;      //开始一个新单词;
                n_words++;      //统计单词
            }
            if (isspace(c) && inword)
                inword = false;      //打到单词的结尾
            prev = c;
        }

        if (prev != ' ')
            n_lines ++;
        printf("字母数目=%ld,单词数=%d,行数=%d,", n_chars, n_words, n_lines);
        return 0;
    }

  • 相关阅读:
    CentOS 7
    CentOS
    CentOS 7
    CentOS 7
    Linux目录结构说明
    CentOS 7
    CentOS 7
    Linux——工具参考篇
    Linux工具进阶
    Selenium——UI自动化测试(2)——How to Download & Install Selenium WebDriver (待续)
  • 原文地址:https://www.cnblogs.com/nictang/p/7561022.html
Copyright © 2011-2022 走看看