簡體   English   中英

程序計算C語言中的字符,行和單詞的數量

[英]Program counting numbers of characters, lines and words in 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 );

這個:

else if (countch++);

完全沒有道理。

你可能是說

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

這至少更接近理智,但請考慮如果輸入是"hello world" (即多個相鄰的空格)將如何處理。

赫拉有幾個觀察結果:

  • c必須是一個int 原因: EOF超出char范圍,因此當前將截斷的值與EOF進行比較,並且比較將失敗。

  • countch應始終增加,請勿將其放在if

  • 您應該接受任何空格字符作為單詞分隔符。 #include <ctype.h>並使用if (iswhite(c))查找單詞邊界。

  • 單詞可以用多個空格分隔。 另外,最后一個單詞后面可能沒有空格。 考慮使用一個標志來跟蹤前一個字符是否為空格,並且僅在前一個char為空格而當前char不是空格時才更新字數。

像這樣嘗試

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

這是我想出的。 不幸的是,該程序花了很長時間才能編譯自動分級器。 我的老師說的是:“程序可能正在等待來自與要求不同的來源的輸入,它會一直等到自動平地機終止您的運行過程,因為它花了太長時間。因此,請確保您使用為任務指定了相同的輸入要求。” 任何建議修復它,不勝感激。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM