菜鸟.... 不太懂得用字符串
如题, 做题目时出了点问题, 运行程式时会自动停止。题目要我们找出最大和最小单词, 当用户输入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); }