关于C语言 找出十个字符串的最大值

来源:百度知道 编辑:UC知道 时间:2024/09/22 01:16:43
#include <stdio.h>
#include <string.h>
main()
{
char s[10],max;
int i;
for(i=0;i<10;i++)
scanf("%s",&s[i]);
max=s[0];
for(i=1;i<10;i++)
if (strcmp(s[i],max)>0) strcpy(max,s[i]);
puts(max);
}

这个是我自己写的,但上机操作发现不对。帮忙改改~或者重新编也好。。谢谢啦!
要含有这样的函数 if (strcmp(s[i],max)>0) strcpy(max,s[i]);

通过你的代码和要求,简单写一下,以后写代码一定要注意编译规范

/*方法一*/
#include <stdio.h>
#include <string.h>
int main()
{
char *s[10];
int i = 0;
printf("下面请根据提示输入10个字符串,最后将输入最大的一个. ");
for (i = 0; i < 10; ++i )
{
s[i] = (char *)malloc(sizeof(char) * 100);
printf("\n请输入第%d个字符串: ", i + 1);
scanf("%s",s[i]);
}

int max_index = 0;
for ( i = 1; i < 10; ++i )
{
if ( strcmp(s[i], s[max_index]) > 0 )
{
max_index = i;
}
}

printf("\n最大的字符串是第%d个: ", max_index + 1);
puts(s[max_index]);

for ( i = 0; i < 10; ++i )
{
free(s[i]);
}

system("pause");

return 0;
}

/*方法二*/
#include <stdio.h>
#include <string.h>
int main()
{
char *s[10];
int i = 0;
print