Hi to all, I've been trying to write one small program, and I got stuck. I mean, it is finished, it compiles, but does not work as it is supposed to. The task is to keep accepting input from user (words, one by one) until the program spots a four-letter word. That's when it stops and displays the smallest and longest word entered so far.
What is wrong? Well, the "count" variable does not record the new value when entering the while loop again at all. It keeps being incremented by next entered word... What's more, the length of smallest/largest word is not counted at all.
Code:
#include <stdio.h>
#include <string.h>
#define LEN 20
int main()
{
int count;
int i = 0;
int count_smallest;
int count_largest;
char word[LEN];
char smallest_word[LEN];
char largest_word[LEN];
char ch;
do {
printf("Enter a word: ");
while( (ch = getchar()) != '\n')
if(i < LEN)
word[i++] = ch;
word[i] = '\0';
count = strlen(word);
count_smallest = strlen(smallest_word);
count_largest = strlen(largest_word);
if(count <= count_smallest)
strcpy(smallest_word, word);
else
strcpy(largest_word, word);
}
while(count != 4);
printf("Smallest word is %d letters long and it is: ",
count_smallest);
puts(smallest_word);
printf("\n");
printf("Largest word is %d letters long and it is: ",
count_largest);
puts(largest_word);
printf("\n");
return 0;
}
I will appreciate your fresh look at my code...