[英]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: 如果按字典顺序排列这四个词, 则会得到:
Thus the output looks correct ("bear" is the first, and "dog" is the last). 因此,输出看起来正确(“ bear”是第一个,“ dog”是最后一个)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.