繁体   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