Iam not getting the desired output, I dont know what is wrong.
/**************************************
** This program allows the user to enter **
** words up to 20 characters long and stops **
** at the first 4 charcter word. It then **
** places the words in ascending order **
** printing the smallest and largest word. **
**************************************/
Code:
#include <stdio.h>
#include <string.h>
#define N 20
void smallest_word(char[][], int );
void largest_word(char[][], int);
int read_line(char[][], int);
int i = 0;
main()
{ int len=0;
char str[N][N];
while (len != 4)
{ printf("Enter word: ");
len = read_line(str, N);
}
smallest_word(str, N);
largest_word(str, N);
return 0;
}
/* read_line */
int read_line(char str[][N], int n)
{ int l=0;
scanf("%s", str[i]);
l = strlen(str[i]);
i++;
if (l >= 20)
{printf("Word too long.\n ");
exit(0);
}
else
{printf("Word is: %s\n", str[i-1]);
return l;
}
}
/* smallest_word */
void smallest_word(char str[][N], int n)
{ int j;
char smallest;
strcpy(&smallest, str[0]);
for(j=1; j<=i; j++)
{ printf("str[%d] = %s\n", j, str[j]);
if (strcmp(str[j], &smallest) < 0)
strcpy(&smallest, str[j]);
}
printf("Smallest word is: %s\n", &smallest);
}
/* largest_word */
void largest_word(char str[][N], int n)
{ int j;
char largest;
strcpy(&largest, str[0]);
for(j=1; j<=i; j++)
{ if (strcmp(str[j], &largest) > 0)
strcpy(&largest, str[j]);
}
printf("Largest word is: %s\n", &largest);
}
Errors being obtained:
Enter word: twelve
Enter word: six
Enter word: fifty
Enter word: four
str[1] = six
Smallest word is: six
Largest word is: t