C 排序最大和最小的字符串
我无法让程序正确地将字符串排序为所有用户输入中的最大值和最小值 我对if语句以及它们如何记录最小和最大的语句有疑问C 排序最大和最小的字符串,c,C,我无法让程序正确地将字符串排序为所有用户输入中的最大值和最小值 我对if语句以及它们如何记录最小和最大的语句有疑问 #include <stdio.h> #include <string.h> #include <conio.h> int main() { int finish = 0, longest=0, smallest=0, count =0; char word[20], smallest_word[20], largest_word[20]; w
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
int finish = 0, longest=0, smallest=0, count =0;
char word[20], smallest_word[20], largest_word[20];
while (0 == finish)
{
printf("enter word:");
fflush(stdin);
scanf("%s", &word);
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
}
if (strcmp(word, smallest_word)<smallest)
{
strcpy(smallest_word, word);
smallest = strcmp(word, smallest_word);
}
if (strcmp(word, largest_word) > longest)
{
strcpy(largest_word, word);
longest = strcmp(word, largest_word);
}
if (strlen(word) == 4)
{
finish++;
}
count++;
}
printf("smallest word: %s\n", smallest_word);
printf("largest word: %s\n", largest_word);
getch();
return 0;
}
#包括
#包括
#包括
int main()
{
int finish=0,最长=0,最小=0,计数=0;
字符字[20],最小的字母字[20],最大的字母字[20];
而(0==完成)
{
printf(“输入单词:”);
fflush(stdin);
scanf(“%s”、&word);
如果(计数=0)
{
strcpy(最小的单词,单词);
strcpy(最大的单词,单词);
}
if(strcmp(字,最小字)最长)
{
strcpy(最大的单词,单词);
最长=strcmp(单词,最大单词);
}
if(strlen(word)==4)
{
finish++;
}
计数++;
}
printf(“最小单词:%s\n”,最小单词);
printf(“最大单词:%s\n”,最大单词);
getch();
返回0;
}
程序运行时无法正确记录有关以下各项的最大值和最小值:
if (strcmp(word, smallest_word)<smallest)
if(strcmp(字,最小字)最长)
strcmp()
返回0或>0或您可以使用string.h
库中的strlen()
查看字符串的大小。您的逻辑和语法都有一些问题
这里,如果两个字符串相同,strcmp(src,dst)返回0,或者在src>dst和dst>src时分别返回某个正数或负数
scanf()需要变量的地址,数组名本身给出了数组第一个元素的地址,因此不需要使用“&”
对于逻辑部分,应该在增加值后继续循环,以避免额外的混淆和比较
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
}
而且不需要最长和最小的变量,因为可以使用零来代替它们
我为您编辑的完整工作代码是
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
int finish = 0, longest=0, smallest=0, count =0;
char word[20], smallest_word[20], largest_word[20];
while (0 == finish)
{
printf("enter word:\t");
scanf("%s", word);
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
count++;
continue; //To ensure in first time loop ends here
}
if (strcmp(word, smallest_word)<0)
strcpy(smallest_word, word);
else if (strcmp(word, largest_word) > 0)
strcpy(largest_word, word);
if (strlen(word) == 4)
{
finish++;
}
count++;
}
printf("smallest word: %s\n", smallest_word);
printf("largest word: %s\n", largest_word);
getch();
return 0;
}
#包括
#包括
#包括
int main()
{
int finish=0,最长=0,最小=0,计数=0;
字符字[20],最小的字母字[20],最大的字母字[20];
而(0==完成)
{
printf(“输入单词:\t”);
scanf(“%s”,单词);
如果(计数=0)
{
strcpy(最小的单词,单词);
strcpy(最大的单词,单词);
计数++;
continue;//确保第一次循环在此处结束
}
if(strcmp(字,最小字)0)
strcpy(最大的单词,单词);
if(strlen(word)==4)
{
finish++;
}
计数++;
}
printf(“最小单词:%s\n”,最小单词);
printf(“最大单词:%s\n”,最大单词);
getch();
返回0;
}
快乐编码:)头文件conio.h
及其getch()
不是标准C的一部分,您可能应该避免使用它们。
见和
您的循环似乎一直持续到用户输入长度正好为4的单词为止。无需为其使用单独的变量,如finish
。只需使用break
语句
而不是
if (strlen(word) == 4)
{
finish++;
}
if (strcmp(word, smallest_word)<smallest)
{
strcpy(smallest_word, word);
smallest = strcmp(word, smallest_word);
}
if (strcmp(word, largest_word) > longest)
{
strcpy(largest_word, word);
longest = strcmp(word, largest_word);
}
做
您可以去掉finish
变量
scanf()
需要指向缓冲区的指针,在缓冲区中,扫描的输入需要作为参数存储在格式字符串之后。与
scanf("%s", &word);
由于C中的数组名会退化为指向其第一个元素的指针,所以将指针指定给一个指针,因为word
本身就是一个指针。因此,word
本身指向存储在word[]
数组中的第一个字符
使用
相反。19是宽度说明符。选择19是因为word
的大小为20
,我们需要1
字符空间来存储表示字符串结尾的\0
。这有助于避免缓冲区溢出
您可能还需要检查scanf()
的返回值。它返回成功分配的数量,如
if( scanf("%19s", &word)!=1 ) {
//Something went wrong.
}
看
在stdin
上使用fflush()
会导致C中未定义的行为,因为它仅用于输出流。看
如果第一个参数小于第二个参数,则返回负数;如果第一个参数大于另一个参数,则返回正数。这些数字可能是任何数字。您无需保存strcmp()的返回值。
所以不是
if (strlen(word) == 4)
{
finish++;
}
if (strcmp(word, smallest_word)<smallest)
{
strcpy(smallest_word, word);
smallest = strcmp(word, smallest_word);
}
if (strcmp(word, largest_word) > longest)
{
strcpy(largest_word, word);
longest = strcmp(word, largest_word);
}
if(strcmp(字,最小字)最长)
{
strcpy(最大的单词,单词);
最长=strcmp(单词,最大单词);
}
做
if(strcmp(字,最小字)<0)
{
strcpy(最小的单词,单词);
}
if(strcmp(字,最大字)>0)
{
strcpy(最大的单词,单词);
}
因此,您可以将程序更改为
char word[20], smallest_word[20], largest_word[20];
for(int count=0; ; ++count)
{
printf("enter word:");
scanf("%19s", word);
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
}
if (strcmp(word, smallest_word) < 0)
{
strcpy(smallest_word, word);
}
if (strcmp(word, largest_word) > 0)
{
strcpy(largest_word, word);
}
if (strlen(word) == 4)
{
break;
}
}
printf("smallest word: %s\n", smallest_word);
printf("largest word: %s\n", largest_word);
char单词[20],最小的单词[20],最大的单词[20];
对于(整数计数=0;;++count)
{
printf(“输入单词:”);
scanf(“%19s”,单词);
如果(计数=0)
{
strcpy(最小的单词,单词);
strcpy(最大的单词,单词);
}
if(strcmp(字,最小字)<0)
{
strcpy(最小的单词,单词);
}
if(strcmp(字,最大字)>0)
{
strcpy(最大的单词,单词);
}
if(strlen(word)==4)
{
打破
}
}
printf(“最小单词:%s\n”,最小单词);
printf(“最大单词:%s\n”,最大单词);
C标准明确规定:fflush(stdin)代码>是未定义的行为,如下所述:scanf(“%s”和&word)代码>这会导致编译器输出消息:“untitled.c:13:13:警告:格式“%s”要求参数类型为“char”,但参数2的类型为“char()[20]”[-Wformat=]”若要解决此问题,请使用:scanf(“%s”,word)
这与引用数组名称会降级为数组第一个字节的地址这一事实有关。此外,1)始终检查返回值(而不是参数值),以确保操作成功。注意:scanf()
函数族在使用格式说明符“%s”和/或“%[…]”时,返回成功的输入格式转换数(cont)(cont)2,并且始终包含一个小于长度的最大字符修饰符
char word[20], smallest_word[20], largest_word[20];
for(int count=0; ; ++count)
{
printf("enter word:");
scanf("%19s", word);
if (count == 0)
{
strcpy(smallest_word, word);
strcpy(largest_word, word);
}
if (strcmp(word, smallest_word) < 0)
{
strcpy(smallest_word, word);
}
if (strcmp(word, largest_word) > 0)
{
strcpy(largest_word, word);
}
if (strlen(word) == 4)
{
break;
}
}
printf("smallest word: %s\n", smallest_word);
printf("largest word: %s\n", largest_word);