fork(2) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define N 20
  5.  
  6. int main( void )
  7. {
  8.  
  9. char max[N];
  10. char min[N];
  11. char (*ch)[N];
  12. char inputStr[N] ;
  13. int length = 0;
  14.  
  15.  
  16. fgets(inputStr, sizeof(inputStr), stdin);
  17.  
  18.  
  19.  
  20. //technique fr: https://stackoverflow.com/a/28462221/701302
  21. inputStr[strcspn(inputStr, "\n")] = 0;
  22.  
  23.  
  24.  
  25. strcpy(max,inputStr);
  26. strcpy(min,inputStr);
  27.  
  28. do
  29. {
  30. fgets(inputStr, sizeof(inputStr), stdin);
  31.  
  32. //technique fr: https://stackoverflow.com/a/28462221/701302
  33. inputStr[strcspn(inputStr, "\n")] = 0;
  34.  
  35. if (strcmp(inputStr,max) > 0){
  36. strcpy(max,inputStr);
  37. }
  38. else
  39. if ( strcmp(inputStr,min) < 0){
  40. strcpy(min,inputStr);
  41. }
  42. length = strlen(inputStr);
  43. } while (length != 4);
  44.  
  45. printf("largest word is: %s\n",max);
  46.  
  47. ch = &min;
  48. if (strcmp(ch,"") == 0){
  49. ch = "[empty string]";
  50. }
  51. printf("smallest word is: %s",ch);
  52.  
  53. }
Success #stdin #stdout 0s 4316KB
stdin
penguin

cat
dog
bear
stdout
largest word is: penguin
smallest word is: [empty string]