程序计算C中的字符,行和单词的数量

时间:2015-05-20 06:54:28

标签: c

我是C的新手,我正在尝试编写一个代码,用于计算C中字符,行和单词的数量。我不知道在哪里继续,如果有人可以请指导我答案,那非常感激。谢谢。我还没有学过任何关于字符串的知识,所以我试图在不使用字符串的情况下做到这一点。

到目前为止,我有这个

int countch =0;
int countwd =0;
int lines = 0;
char c;

printf("Enter text:");
while((c = getchar()) != EOF) {

if(c == ' ')
countwd++;
else if (countch++);
else if(c == '\n') {
    lines++;

putchar(c);
}


printf("The amount of characters is %d\n The amount of words is %d\n The amount of lines is %d", countch, countwd,lines );

4 个答案:

答案 0 :(得分:1)

此:

else if (countch++);

毫无意义。

你可能意味着

if(c == ' ')
  countwd++;
else if(c == '\n')
  lines++;
else
  countch++;

至少接近理智的东西,但要考虑输入为"hello world"时会发生什么,即多个相邻的空间。

答案 1 :(得分:1)

Hera有几点意见:

  • c必须是int。原因:EOF超出了char的范围,因此目前,截断值将与EOF进行比较,比较将失败。

  • countch应始终增加,不要将其放在if

  • 您应该接受任何空格字符作为单词分隔符。 #include <ctype.h>并使用if (iswhite(c))查找字边界。

  • 单词可以由多个空格分隔。此外,最后一个单词后面可能没有任何空格。考虑使用标记来跟踪前一个字符是否为空格,并且仅在前一个char为空格且当前char不是空白时才更新字数。

    < / LI>

答案 2 :(得分:0)

尝试这样

 Switch(c)
{
 case ' ':
    countwd++;
    break;
 case '\n':
    lines++;
    break;
 default:
    countch++;
}

答案 3 :(得分:0)

这是我想出的。不幸的是,该程序为自动分级器编译需要很长时间。以下是我的老师所说的“程序可能正在等待来自不同于需求的源的输入,它会一直等到自动分级器因为花费太长时间而终止运行过程。所以请确保使用以下内容测试程序为任务指定的相同输入要求。“ 任何有关修复它的建议都表示赞赏。

//printf( "%lu %lu %lu\n", charcount, wordcount, linecount );
/*Write a C program called count.c that counts the number of characters, words and lines read from standard input until EOF is reached.
Assume the input is ASCII text of any length.
Every byte read from stdin counts as a character except EOF.
Words are defined as contiguous sequences of letters(a through z, A through Z) and the apostrophe(', value 39 decimal) 
separated by any character outside these ranges.
Lines are defined as contiguous sequences of characters separated by newline characters('\n').
Characters beyond the final newline character will not be included in the line count.
On reaching EOF, use this output command :
printf("%lu %lu %lu\n", charcount, wordcount, linecount);
Where charcount, wordcount and linecount are all of type unsigned long int.You may need these large types to handle long documents.*/
#include<stdio.h>
#include<string.h>

int main(void)
{
    unsigned long int charcount = 0, wordcount = 0, linecount = 0;
    int c;//getchar converts to int so use this

    while ((c!= EOF)//count every byte except EOF for characters
    {
        c = getchar();
        charcount++;
    }

    char word[1000] = { 0 };//buffer; never seen a word longer than this

    int i = 0;
    for (i = 0; i < 1000; i++)
    {
        char word[1000] = { 0 };//buffer initialized to zero
        if ((getchar() >= 'A') && (getchar() <= 'Z') || (getchar() >= 'a' && getchar() <= 'z') || ((getchar() == '\'')))//any of these get put into array
            word[i] = getchar();
        else if ((getchar() != ' ') || (getchar() != '\n'))
        {
            wordcount++;
            memset(word, 0, sizeof(word));//clear the buffer
            i = 0;
            continue;
        }
        else if (getchar() == EOF)
        {
            wordcount++;
            break;
        }
        else
        {
            wordcount++;
            memset(word, 0, sizeof(word));
            i = 0;
            continue;
        }
    }


    char line[50000] = { 0 };
    for (i = 0; i < 50000; i++)
    {
        line[i] = getchar();
        if (getchar() == '\n')
        {
            linecount++;
            memset(line, 0, sizeof(line));
            i = 0;
            continue;
        }
        else if (getchar() == EOF)
        {
            linecount++;
            break;
        }
        else
        {
            linecount++;
            memset(line, 0, sizeof(line));
            i = 0;
            continue;
        }
    }
    printf("%lu %lu %lu\n", charcount, wordcount, linecount);


    return 0;
}