Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAX 50
  6. #define SIZE 10
  7.  
  8. void myFgets(char names [][MAX] , int len );
  9. void longest (char names [][MAX] , int len);
  10. void shortest (char names [][MAX] , int len);
  11. void alpha(char names [][MAX] , int len);
  12.  
  13. int main (void)
  14. {
  15.  
  16. char names [SIZE][MAX] = {0};
  17. myFgets(names , SIZE);
  18. shortest(names , SIZE);
  19. longest(names , SIZE);
  20. alpha(names , SIZE);
  21.  
  22.  
  23. return (0);
  24. }
  25.  
  26.  
  27.  
  28.  
  29. void myFgets(char names [][MAX] , int len )
  30. {
  31.  
  32. int i = 0;
  33. for(i=0; i<len; i++)
  34. {
  35. fgets(names[i] , MAX , stdin);
  36. names[i][strcspn(names[i],"\n")]=0;
  37. }
  38. }
  39.  
  40.  
  41.  
  42. void longest (char names [][MAX] , int len)
  43. {
  44. int max = 0;
  45. int i = 0;
  46. int index = 0;
  47. for(i=0; i<len; i++)
  48. {
  49. if(max<strlen(names[i]))
  50. {
  51. max = strlen(names[i]);
  52. index = i;
  53. }
  54. }
  55. printf("longest name is: %s\n" , names[index]);
  56.  
  57.  
  58.  
  59. }
  60.  
  61.  
  62.  
  63. void shortest (char names [][MAX] , int len)
  64. {
  65. int min = 51;
  66. int i = 0;
  67. int place = 0;
  68.  
  69. for(i=0; i < len; i++)
  70. {
  71. if(min > strlen(names[i]))
  72. {
  73. min = strlen(names[i]);
  74. place= i;
  75. }
  76. }
  77. printf("shortest name is: %s\n" , names[place]);
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85. void alpha(char names [][MAX] , int len)
  86. {
  87. int first = 0;
  88. int last = 0;
  89. int i = 0;
  90. for(i = 0; i<len; i++)
  91. {
  92. if(strcmp(names[i] , names[first])<0)
  93. {
  94. first = i;
  95. }
  96. if(strcmp(names[i] , names[last])>0)
  97. {
  98. last = i;
  99. }
  100. }
  101. printf("first name is: %s\n" , names[first]);
  102. printf("last name is: %s\n" , names[last]);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement