简体   繁体   English

查找最小和最大单词的程序

[英]program to find the smallest and largest word

This is an example from KN King book which finds the smallest and largest word in a series of words and stops at word length of 4. But it doesn't work correctly. 这是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;
}

Suppose I type: 假设我输入:

cat 
dog 
catfish
bear

gives

Output:
Smallest Word: bear
Largest Word: dog

which I think is wrong. 我认为这是错误的。

If we arrange the four words in the lexicographic order , we get: 如果按字典顺序排列这四个词, 则会得到:

  • bear
  • cat
  • catfish 鲶鱼
  • dog

Thus the output looks correct ("bear" is the first, and "dog" is the last). 因此,输出看起来正确(“ bear”是第一个,“ dog”是最后一个)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找三个输入数字中最大和最小的程序,并显示识别出的最大/最小数字是偶数还是奇数 - program to find the largest and smallest among three entered numbers and also display whether the identified largest/smallest number is even or odd 使用数组编写C程序,以从100个随机数的列表中找到最大和最小的数 - Write a C program using array to find largest and smallest number from a list of 100 Random numbers 如何在C中找到最大和最小的数字 - How to find largest and smallest number in c c - 如何在数组中找到最大和最小的数字 - How to find the largest and smallest number in an array in c 仅使用 4 个“if”语句 - 查找 4 个整数中的最大和最小 - Using only 4 'if' statements - Find Largest and Smallest of 4 integers 寻找数组中最大和第二大数的程序 - Program to find largest and second largest number in array 在这个程序中遇到一些错误,特别是最大和最小的 function - Getting a few errors in this program specifically with the largest and the smallest function 用C语言编写代码以从文本文件中查找最大和最小数字 - Code in C to find largest and smallest numbers from text file 在 C 中查找停止在 0 处的数组的最大、最小、总和和平均值 - Find the largest, smallest, sum and average of an array that stops at 0 in C 如何从输入整数中找到最大和最小可能的数字? - How to find the largest and smallest possible number from an input integer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM