微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

计算字符,单词,行数

#include <stdio.h>
#include <ctype.h>         // for isspace()
#include <stdbool.h>       // for bool, true, false

#define STOP '|'
int main(void)
{
    char c;                 
    char prev;              // prevIoUs character read
    long charCount = 0L;      
    int lineCount = 0;        
    int wordCount = 0;        
    int partialLineCount = 0; 
    bool inword = false;    // == true if c is in a word

    printf(Enter text to be analyzed (| to terminate):\n);
    prev = '\n';            // used to identify complete lines
    while ((c = getchar()) != STOP)
    {
        charCount++;          // count characters
        if (c == '\n')
            lineCount++;      // count lines
        if (!isspace(c) && !inword)
        {
            inword = true;  // starting a new word
            wordCount++;      // count word
        }
        if (isspace(c) && inword)
            inword = false; // reached end of word
        prev = c;           // save character value
    }

    if (prev != '\n')
        partialLineCount = 1;
    printf(characters = %ld, words = %d, lines = %d, ,
           charCount, wordCount, lineCount);
    printf(partial lines = %d\n, partialLineCount);

    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐