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;
    }

  • 相关阅读:
    Python3 之 列表推导式
    python3 之 趣味数学题(爱因斯坦)
    python3 之 判断闰年小实例
    python3 之 判断字符串是否只为数字(isdigit()方法、isnumeric()方法)
    116.Populating Next Right Pointers in Each Node
    115.Distinct Subsequences
    114.Flatten Binary Tree to Linked List
    113.Path Sum II
    112.Path Sum
    111.Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/nictang/p/7561022.html
Copyright © 2011-2022 走看看