如何用纯C计算单词、字符和行数

如何用纯C计算单词、字符和行数,c,C,我想数一数单词、字符、新行的数量,不管我的句子看起来如何。(例如,即使我键入这样的句子: y yafa\n\n youasf\n sd 程序仍应能够正确计算字数、行数和字符数)。我不知道如何用纯C实现这样的程序,有人能帮我吗 这是我当前的代码,它只能在某些条件下正确 int main() { int cCount = 0, wCount = 0, lCount = 0; printf("Please enter the sentence you want\n"); c

我想数一数单词、字符、新行的数量,不管我的句子看起来如何。(例如,即使我键入这样的句子:

y yafa\n\n youasf\n sd

程序仍应能够正确计算字数、行数和字符数)。我不知道如何用纯C实现这样的程序,有人能帮我吗

这是我当前的代码,它只能在某些条件下正确

int main() {
    int cCount = 0, wCount = 0, lCount = 0;

    printf("Please enter the sentence you want\n");

    char str[20];
    int j7 = 0;

    while (int(str[j7]) != 4) {
        str[j7] = getchar();
        if (str[0] == ' ') {
            printf("Sentence starts from a word not blank\n");
            break;
        }
        if (int(str[j7]) == 4)
            break;
        j7++;
    }
    int count = 0;
    while (count < 20) {
        if (str[count] != ' ' && str[count] != '\n') {
            cCount++;
        } else
        if (str[count] == ' ') {
            wCount++;
        } else
        if (str[count] == '\n') {
            lCount++;
        }
        count++;
    }

    printf("Characters: %d\nWords: %d\nLines: %d\n\n",
           cCount, wCount++, lCount + 1);
    int x = 0;
    std::cin >> x;
}
intmain(){
int cCount=0,wCount=0,lCount=0;
printf(“请输入您想要的句子\n”);
char-str[20];
int j7=0;
while(int(str[j7])!=4){
str[j7]=getchar();
如果(str[0]=''){
printf(“句子从一个非空单词开始\n”);
打破
}
if(int(str[j7])==4)
打破
j7++;
}
整数计数=0;
同时(计数<20){
如果(str[count]!=''&&str[count]!='\n'){
cCount++;
}否则
如果(str[计数]=''){
wCount++;
}否则
如果(str[count]='\n'){
lCount++;
}
计数++;
}
printf(“字符:%d\n单词:%d\n行:%d\n\n”,
Account、wCount++、lCount+1);
int x=0;
标准:cin>>x;
}
<>代码> 您不是用纯C编写,而是用C++来编写。
为了实现目标,您必须将问题总结为一系列逻辑步骤:

  • 对于读取的每个字符:
    • 如果前一行是行分隔符,则有新行
    • 如果前一个是单词分隔符,而当前不是,则有一个新词
    • 如果所有情况下,您都有一个新字符,请将其保存为下一次迭代的前一个字符
使用
'\n'
作为
最后一个
字符的初始值,因此读取的第一个字符(如果有)将开始一个新行,如果不是空白,则可能开始一个新词

下面是一个简单的实现:

#include <ctype.h>
#include <stdio.h>

int main(void) {
    long chars = 0, words = 0, lines = 0;

    printf("Enter the text:\n");

    for (int c, last = '\n'; (c = getchar()) != EOF; last = c) {
        chars++;
        if (last == '\n')
            lines++;
        if (isspace(last) && !isspace(c))
            words++;
    }
    printf("Characters: %ld\n"
           "Words: %ld\n"
           "Lines: %ld\n\n", chars, words, lines);
    return 0;
}

纯C
没有
std::cin
,所以不要与C++混合,也要尝试使用好的变量名,而不是像
j7
那样,它并不意味着什么。对于代码的审查,如果一个文件只有3个字符:
“abc”
,并且没有以下换行符,那么你可能会有更好的机会,您希望得到什么样的结果?
int(str[j7])
不是纯C构造。使用
(int)(str[j7])
@4386427:事实上,我相信它是这样的:
两个\nwords
将被计算为9个字符,2个单词,2行,这可以说比
wc
的输出更正确:
12 9
@D。Kenny:将
for
循环转换为更容易出错的
循环。
#include <ctype.h>
#include <stdio.h>

int main(void) {
    long chars = 0, words = 0, lines = 0;
    int c, last = '\n';

    printf("Enter the text:\n");

    while ((c = getchar()) != EOF) {
        chars++;
        if (last == '\n')
            lines++;
        if (isspace(last) && !isspace(c))
            words++;
        last = c;
    }
    printf("Characters: %ld\n"
           "Words: %ld\n"
           "Lines: %ld\n\n", chars, words, lines);
    return 0;
}