作业帮 > 综合 > 作业

c语言:编写一个程序找出一组单词中的“最小“和“最大“的单词(单词在字典中的先后顺序,字典中先出现的小).当输入四个字母

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/03/29 22:25:37
c语言:编写一个程序找出一组单词中的“最小“和“最大“的单词(单词在字典中的先后顺序,字典中先出现的小).当输入四个字母单词时,程序停止读入.我这里已经写好了,有一个小问题,就是当我输入dog,zebra,rabbit,catfish,walrus,cat,fish.时,应该输出的正确答案是small cat,large zebra.但我输出的small 是我dog.
#include
#include
#define N 20
int main()
{
\x05char smallest_word[N],largest_word[N],a[N],b[N];
\x05int i,k;
\x05printf("Enter word:");
\x05gets(b);
\x05strcpy(smallest_word,b);
\x05strcpy(largest_word,b);
\x05k = strlen(b);
\x05if (k == 4)
\x05{
\x05\x05printf("Smallest word:");
\x05\x05puts(smallest_word);
\x05\x05printf("Largest word:");
\x05\x05puts(largest_word);
\x05\x05return 0;
\x05}
\x05else
\x05{
\x05\x05for (;;)
\x05\x05{
\x05\x05\x05printf("Enter word:");
\x05\x05\x05gets(a);
\x05\x05\x05k = strlen(a);
\x05\x05\x05if (k = 4)
\x05\x05\x05{
\x05\x05\x05\x05if (strcmp(smallest_word,a) > 0)
\x05\x05\x05\x05\x05strcpy(smallest_word,a);
\x05\x05\x05\x05if (strcmp(largest_word,a) < 0)
\x05\x05\x05\x05\x05strcmp(largest_word,a);/*我觉得是这一步没有运行*/
\x05\x05\x05}
\x05\x05\x05else
\x05\x05\x05{
\x05\x05\x05\x05printf("Smallest word:");
\x05\x05\x05\x05puts(smallest_word);
\x05\x05\x05\x05printf("Largest_word ");
\x05\x05\x05\x05puts(largest_word);
\x05\x05\x05\x05return 0;
\x05\x05\x05}
\x05\x05}
\x05}
}
谢谢帮我检查下!
strcmp(largest_word,a);/*我觉得是这一步没有运行*/
你这一行写错了吧,不是应该strcpy么,怎么还是strcmp呢?
再问: 呵呵,是我傻了,我竟然检查一晚上没有查出。。。。