1

これは 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

私は間違っていると思います。

4

1 に答える 1

5

4 つの単語を辞書順に並べると、次のようになります。

  • くま
  • ナマズ

したがって、出力は正しいように見えます (「bear」が最初で、「dog」が最後です)。

于 2013-03-11T10:21:26.467 に答える