Hi the program is not really very efficient unless it is a class project, as it only allows words up to 3 characters. Still if this is what you require...who am I to say otherwise.
Here are a few alterations
Code:
/*you dont need to return a value from function*/
void read_line(char str[]); /*and you dont need LENGTH*/
main()
{
/*intialise variable with largest lexicographical combination*/
/*so it will change*/
char largest[LENGTH] = "z"; /*lower z*/
/*same here*/
char smallest[LENGTH] = "zzz";
char input[LENGTH];
int len;
printf("Enter word: ");
read_line(input);
len = strlen(input);
/*you could lose the if statement and for loop with a while loop*/
while(len < 4)
{
if (strcmp(input, smallest) < 0)
/*strcpy here will replace the smallest if lexicographically*/
/*smaller than stored value in smallest*/
strcpy(smallest, input);
else if (strcmp(input, largest) > 0)
/*strcpy here will replace the larger if lexicographically*/
/*larger than stored value in largest*/
strcpy(largest, input);
printf("Enter word: ");
read_line(input);
len = strlen(input);
}
/*syntax error in this printf fnct missing " */
printf("\n\n\nYou input four-letter word\n);
printf("So time to quit.\n\n");
printf("Smallest word: %s\n", smallest);
printf("Largest word: %s\n", largest);
return 0;
}
void read_line(char str[])
{
char ch;
int i = 0;
while ((ch = getchar()) != '\n')
if (i < LENGTH) /*LENGTH global when #define*/
str[i++] = ch;
str[i] = '\0'; /*no return value*/
}
This may save you from reposting...as I said the program is not challenging when only 3 chars can be input....The following allows the user to input any sentance up to 80 chars and sorts longest and shortest then sorts alphabetically (lexicographically) when sentances are of same length...
Code:
#include <stdio.h>
#include <string.h>
#define MAX 80
void main ()
{
char in_string[MAX],
small[MAX],
big[MAX]="z"; /*initialise big string so it will change*/
int i;
/*initialise small string so it will change*/
for(i = 0; i < MAX - 1; i++)
small[i]= 'z';
small[i]='\0';
printf("\n\tEnter a sentance - Hit return to quit\n\t");
gets(in_string);
while(*in_string)
{
fflush(stdin);
if(strlen(in_string)< strlen(small))
strcpy(small, in_string);
else if(strlen(in_string)== strlen(small))
if(strcmp(small, in_string)< 0)
strcpy(small, in_string);
if(strlen(in_string)> strlen(big))
strcpy(big, in_string);
else if(strlen(in_string)== strlen(big))
if(strcmp(big, in_string)> 0)
strcpy(big, in_string);
printf("\n\tEnter a sentance - Hit return to quit\n\t");
gets(in_string);
}
printf("\n\tSmallest Sentance is \n\t\"%s\"", small);
printf("\n\tLargest Sentance is \n\t\"%s\"", big);
}