Q.WAP to find the smallest and largest word that a user entered in C language. The program needs to terminate when the user ente


/ Published in: C
Save to your folder(s)

Q.WAP to find the smallest and largest word that a user entered in C language. The program needs to terminate when the user enters a four lettered word. (Question from K N King C modern approach)


Copy this code and paste it in your HTML
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int count = 0; // To count the number of words
  5. char smallest_word[20]; //To store the smallest word
  6. char largest_word[20]; // To store the largest word
  7. void print_words(char phrase[10][20]);// to print all the words
  8. void get_words(char phrase[10][20]);//to get the words from the user
  9. void compare_words(char phrase[10][20]);//function to find the smallest and the largest word in the group.
  10.  
  11. int main()
  12. {
  13. char phrase[10][20];
  14. printf("Enter Words (Enter A 4 letter Word to terminate) \n");
  15. get_words(phrase);
  16. compare_words(phrase);
  17. printf("All the Words Entered Are :\n");
  18. print_words(phrase);
  19. printf("The Largest And Smallest Words Are : \n%s \n%s",largest_word,smallest_word);
  20. return 0 ;
  21. }
  22.  
  23. void print_words(char phrase[10][20])
  24. {
  25. for(int i = 0 ;i<count;i++)
  26. {
  27. printf("%s\n",phrase[i]);
  28. }
  29. }
  30.  
  31. void get_words(char phrase[10][20])
  32. { int length_str = 0;
  33. for(int i = 0 ;length_str!=4;i++)
  34. {
  35. scanf("%s",&phrase[i]);
  36. length_str = strlen(phrase[i]);
  37. ++count;
  38. }
  39. }
  40.  
  41. void compare_words(char phrase[10][20])
  42. {
  43. strcpy(smallest_word,phrase[0]);
  44. strcpy(largest_word,phrase[0]);
  45. for(int i = 1;i<count;i++)
  46. {
  47. if(strcmp(smallest_word,phrase[i])>0)
  48. {
  49. strcpy(smallest_word,phrase[i]);
  50. }
  51. else if(strcmp(largest_word,phrase[i])<0)
  52. {
  53. strcpy(largest_word,phrase[i]);
  54. }
  55. }
  56. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.