![]() |
#2
qq10235692232011-04-23 07:41
|
题目要我们找出最大和最小单词, 当用户输入4个字母的单词,程式便停止读入。
例子:
Enter the word: dog
Enter the word: zebra
Enter the word: rabbit
Enter the word: cat
Enter the word: fish
Smallest word: cat
Biggest word: zebra

#include<stdio.h>
#include<string.h>
#define N 100
void main()
{
char smallest[N]={0L},biggest[N]={0L};
char input[N];
int i,length;
do
{
printf("Enter the word: ");
for (i=0;i<N;i++)
{
scanf("%c",&input[i]);
if (input[i]=='\n')
break;
}
printf("\n\n");
length=strlen(input);
if (strcmp(smallest,input)>=0)
strcpy(smallest,input);
if (strcmp(biggest,input)<0)
strcpy(biggest,input);
}
while (length!=4);
printf("\nThe smallest word is %c",smallest);
printf("\nThe biggest word is %c",biggest);
}
#include<string.h>
#define N 100
void main()
{
char smallest[N]={0L},biggest[N]={0L};
char input[N];
int i,length;
do
{
printf("Enter the word: ");
for (i=0;i<N;i++)
{
scanf("%c",&input[i]);
if (input[i]=='\n')
break;
}
printf("\n\n");
length=strlen(input);
if (strcmp(smallest,input)>=0)
strcpy(smallest,input);
if (strcmp(biggest,input)<0)
strcpy(biggest,input);
}
while (length!=4);
printf("\nThe smallest word is %c",smallest);
printf("\nThe biggest word is %c",biggest);
}