Advertisement
Guest User

Untitled

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