计算字符,单词,行数

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

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/266663.html

(0)
上一篇 2022年6月7日 23:10
下一篇 2022年6月7日 23:10

相关推荐

发表回复

登录后才能评论