3.9k views
3 votes
Write a program findStr.c that finds the "smallest" and "largest" in a series of words. After the user enters the words, the program will determine which words would come first and last if the words were listed in dictionary order. The program must stop accepting input when the user enters a four-letter word. Assume that no word is more than 20 letters long. An interactive session with the program might look like this: Enter word: dog Enter word: zebra Enter word: rabbit Enter word: catfish Enter word: walrus Enter word: cat Enter word: fish Smallest word: cat Largest word: zebra Hint: Use two strings named smallest_word and largest_word to keep track of the "smallest" and "largest" words entered so far. Each time the user enters a new word, use strcmp to compare it with smallest_word; if the new word is "smaller", use strcpy to save it in smallest_word. Do a similar comparison with largest_word. Use strlen to determine when the user has entered a four- letter word.

User Mvilrokx
by
7.2k points

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

C code:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int main()

{

char input[21],smallest_word[21],largest_word[21];

printf("Enter Word : ");

scanf("%s",input);

strcpy(smallest_word,input);

strcpy(largest_word,input);

while(strlen(input)!=4)

{

if(strcmp(input,smallest_word)<0)

strcpy(smallest_word,input);

else if (strcmp(input,largest_word)>0)

strcpy(largest_word,input);

printf("Enter Word : ");

scanf("%s",input);

}

printf("Smallest word : %s\\",smallest_word);

printf("Largest word : %s\\",largest_word);

return 0;

}

Write a program findStr.c that finds the "smallest" and "largest&quot-example-1
User JJSmith
by
7.8k points