-1

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

pratama
  • 45
  • 8
  • you don't initialize c or d. Please read your compiler warnings and fix them – stark Dec 07 '20 at 12:01
  • 1
    Note that [The `gets()` function is so dangerous that it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) It is no longer a part of Standard C. Any teacher teaching you to use it needs to go back to relearn remedial (modern) C. Granted, using `char x[n][1000];` gives an array big enough that you're unlikely to run into overflows, but even that size of array doesn't remove the possibility. – Jonathan Leffler Dec 07 '20 at 12:48
  • The functions `Max()` and `Min()` should be more similar (there's no need for `len1` vs `len2`, nor `c` versus `d`; differential capitalization of `Max` vs `min` in the functions), and neither function should print anything. Rather, the functions should return the index of the longest or shortest string and the calling code (`main()`) should handle the printing. Separating I/O (printing) from 'calculations' is a basic technique of good programming. It makes the calculation more nearly reusable in other programs — not yet a primary concern of yours, but it will become one over time. – Jonathan Leffler Dec 07 '20 at 13:00
  • It's also not a particularly good idea to use the name of the function as the name of a variable within the function — even if the intent is to ensure that recursion is impossible. You have a local variable `Max` within function `Max()` — your skin should be crawling as if there are bugs trying to creep over it. – Jonathan Leffler Dec 07 '20 at 13:02
  • whether in my min function is correct it will produce the shortest string?. im not sure about my program – pratama Dec 07 '20 at 13:08
  • Your `Min()` function will not reliably produce the correct answer if the first entry in the array (index 0) is the shortest string. Similarly, your `Max()` function will not reliably produce the correct answer if the first entry in the array (index 0) is the longest string. – Jonathan Leffler Dec 07 '20 at 13:09
  • what if the shortest word was on another line? what should i do to change it ? @JonathanLeffler – pratama Dec 07 '20 at 13:11
  • Which part of the [answer](https://stackoverflow.com/a/65181402/15168) by [Vlad from Moscow](https://stackoverflow.com/users/2877241/vlad-from-moscow) do you not understand? – Jonathan Leffler Dec 07 '20 at 13:21
  • i understand it but in my min function there is an error like it's only show the first line who same to the longest sentence – pratama Dec 07 '20 at 13:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225624/discussion-between-pratama-and-jonathan-leffler). – pratama Dec 07 '20 at 13:26

2 Answers2

0

Neither the variable c nor the variable d was initialized in the functions Max and Min.

int i,Max,len1,c;

and

int i,min,len2,d;

At least change these declarations like

int i,Max,len1,c = 0;

and

int i,min,len2,d = 0;

You are entering n + 1 elements in this loop

for(i=0;i<=n;i++){

but passing to the functions only the value n instead of n + 1.

Pay attention to the fact that the function gets is so unsafe that it is not supported by the C Standard any more. Instead, use the function fgets.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Try this 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=0; // c=0 (in case x[0] is the max) otherwise x[c] is undefined
 Max=strlen(x[0]);
 for(i=1;i<n;i++){ // i<n because I starts from 0
    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=0; // d=0 otherwise if min=x[0], x[d]is undefined
 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,n,c;
 printf("How many name to accept: ");
 scanf("%d",&n);
 char x[n][1000];
 printf("\nEnter %d words: \n",n);
 while ((c = getchar()) != '\n' && c != EOF) { }
 for(i=0;i<n;i++){ // i<n because i starts from 0 (and not from 1)
    fgets(x[i],sizeof x[i],stdin); // fgets is more secure than gets
    

 }

 Max(x,n);
 Min(x,n);

 return 0;
}
nissim abehcera
  • 821
  • 6
  • 7
  • 1
    And are you going to explain what you changed and why you changed it? I know that the variables `jmlh` and `z` are in the question, but what benefit or service do they provide, other than being a source of warnings about unused variables when the code is compiled with sensible options? Remember, too, that `fgets()` includes the newline in the buffer. You probably need to remove it: `x[i][strcspn(x[i], "\n")] = '\0';` is a good way to do that. – Jonathan Leffler Dec 07 '20 at 13:40
  • while ((c = getchar()) != '\n' && c != EOF) { } can you explain this – pratama Dec 07 '20 at 13:46
  • i don't understand in line: while ((c = getchar()) != '\n' && c != EOF) { } for(i=0;i – pratama Dec 07 '20 at 13:47
  • while ((c = getchar()) != '\n' && c != EOF) { } . This instruction allows to remove newlines in the buffer. Otherwise, when you input a name, the next input will be newline and not the next name , do you understand me ? – nissim abehcera Dec 07 '20 at 13:54
  • can you write it in simple c code statement cause i don't understand @nissimabehcera – pratama Dec 07 '20 at 13:57
  • @pratama while ((c = getchar()) != '\n' && c != EOF) { } => while newline or end of the stream aren't reached, the program continue to remove character in the buffer – nissim abehcera Dec 07 '20 at 14:29