I try to make a program to find the shortest and longest string using function and array but the program doesn't work. the program does not display the function ordered. here my code :
#include<stdio.h>
#include<string.h>
void Max(char x[][1000], int n);
void Min(char x[][1000],int n);
void Max(char x[][1000], int n){
int i,Max,len1,c;
Max=strlen(x[0]);
for(i=1;i<n;i++){
len1=strlen(x[i]);
if(len1>Max)
{
c=i;
Max=len1;
}
}printf("\nthe longest string among all is \"%s\" \n \n",x[c]);
}
void Min(char x[][1000],int n){
int i,min,len2,d;
min=strlen(x[0]);
for(i=1;i<n;i++){
len2=strlen(x[i]);
if(len2<min)
{
d=i;
min=len2;
}
}
printf("\n the shortest string among all is \"%s\" \n \n",x[d]);
}
int main(){
int i,jmlh=0,n,z;
printf("How many name to accept: ");
scanf("%d",&n);
char x[n][1000];
printf("\nEnter %d words: \n");
for(i=0;i<=n;i++){
gets(x[i]);
}
Max(x,n);
Min(x,n);
return 0;
}
the input is
3
and input :
robin van persie
lionel messi
ronaldo
and should an output like this :
the longest string among all is "robin van persie".
the shortest string among all is "ronaldo".
maybe anybody wants to help me to fixed this program and I really need your opinion. thank you