簡體   English   中英

查找最小和最大單詞的程序

[英]program to find the smallest and largest word

這是KN King書中的一個示例,該示例在一系列單詞中找到最小和最大的單詞,並以4個單詞長度停止。但是它不能正常工作。

#include <stdio.h>
#include <string.h>

#define N 20


int main(void) {
    char smallest_word[N];
    char largest_word[N];
    char current_word[N];
    printf("Enter word: ");
    gets(current_word);
    strcpy(smallest_word, strcpy(largest_word, current_word));
    while(strlen(current_word) != 4){
        printf("Enter word: ");
        gets(current_word);
        if(strcmp(current_word, smallest_word) < 0)
            strcpy(smallest_word, current_word);
        if(strcmp(current_word, largest_word) > 0)
            strcpy(largest_word, current_word);

    } 
    printf("\nSmallest word: %s\n", smallest_word);
    printf("Largest word: %s\n", largest_word);
    return 0;
}

假設我輸入:

cat 
dog 
catfish
bear

Output:
Smallest Word: bear
Largest Word: dog

我認為這是錯誤的。

如果按字典順序排列這四個詞, 則會得到:

  • 鯰魚

因此,輸出看起來正確(“ bear”是第一個,“ dog”是最后一個)。

暫無
暫無

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

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