C语言,输入5个字符串,按由小到大的顺序输出。非常急,求指导!

/*输入5个字符串,按由小到大的顺序输出。*/
#include<stdio.h>
#include<string.h>
int main(void){
int i,j;
char a[5][20], t[5];
printf("Enter 5 strings:\n");
for (i = 0; i < 5; i++){
scanf_s("%s",a[i]);
}
for (i = 1; i < 5; i++){
for (j =0; j < 5-i ;j++)
if (strcmp(a[j], a[j+1]) > 0){
strcpy_s(t,20,a[j]);
strcpy_s(a[j],20, a[j + 1]);
strcpy_s(a[j + 1],20, t);
}
}
printf("After sorted:\n");
for (i = 0; i < 5; i++)//<----------------------------不懂的地方在这里!整个程序编译完后都显示没问题,
puts(a[i]); 但是最后的结果就是输出不了,每次输出显示都只有5个
return 0; 换行符,根本没有字符串,这是为什么?求指教!

}

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

void sort(char **p)

{

char **q, **s, *t;

for (q = p; q < p + 4; q++)

{

for (s = q + 1; s < p + 5; s++)

{

if (strcmp(*q, *s) > 0)

{

t = *q;

*q = *s;

*s = t;

}

}

}

}

int main()

{

char *a[5], b[5][99], **p;

int i;

for (i = 0; i < 5; i++)

a[i] = b[i];

printf("请依次输入五个字符串:\n");

for (i = 0; i < 5; i++)

scanf("%s", a[i]);

p = a;

sort(p);

printf("排序后输出为:\n");

for (i = 0; i < 5; i++)

{

printf("%s\n", a[i]);

}

system("pause");

return 0;

}

运行效果:

扩展资料:

scanf函数用法:

scanf("输入控制符",输入参数);

功能:将从键盘输入的字符转化为“输入控制符”所规定格式的数据,然后存入以输入参数的值为地址的变量中。

用scanf()函数以%s格式读入的数据不能含有空白符时,所有空白符都被当做数据结束的标志。所以题中函数输出的值只有空格前面的部分。

如果想要输出包括空格在内的所有数据,可以使用gets()函数读入数据。gets()函数的功能是读取字符串,并存放在指定的字符数组中,遇到换行符或文件结束标志时结束读入。换行符不作为读取串的内容,读取的换行符被转换为字符串结束标志'\0'。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-29

我把scanf_s和strcpy_s改了下就好了。。,你那个拷贝的问题,t数组的长度不够。

长度小于5的话还能用用,大于5的话需要变成t[20]。

/*输入5个字符串,按由小到大的顺序输出。*/
#include<stdio.h>
#include<string.h>
int main(void)
{
    int i,j;
    char a[5][20], t[20];//改大了点,不然字符串太长会超出数组范围
    printf("Enter 5 strings:\n");
    for (i = 0; i < 5; i++)
    {
        scanf("%s",a[i]);
    }
    for (i = 1; i < 5; i++)
    {
        for (j =0; j < 5-i ;j++)
        if (strcmp(a[j], a[j+1]) > 0)
        {
        strcpy(t,a[j]);
        strcpy(a[j], a[j + 1]);
        strcpy(a[j + 1], t);
        }
    }
    printf("After sorted:\n");
    for (i = 0; i < 5; i++)
    puts(a[i]);
    return 0;
}

追问

我用的是microsoft visual studio2013,那个里面要求把scanf写成scanf_s形式,把strcpy写成strcpy_s形式,不然就会报错。还有我已经把t[5],改成t[20]了,运行了一下,还是不对,输出的还是换行符,这是怎么回事?

追答

没用过VS。。我还以为两者可以共用呢。。
你试试输入每个字符用回车结束(我猜可能是由于它是整行读入的,如果没有限制大小的话)。
标准的scanf_s写法是scanf("%s",a[i],Max)。MAX用以确认输入长度限制。

追问

这样啊,明白了,谢谢!我可以问一下你用的是什么吗?我之所以用这个是因为WIN8.1系统与之前的visual c++不兼容,但是用这个感觉好麻烦,所以想试下别的~

追答

我用的是codeblocks

本回答被提问者采纳
第2个回答  2014-08-11
 #include<stdio.h>
#include<string.h>
int main(void)
{
 int i,j;
 char a[5][20], t[20];
 for (i = 0; i < 5; i++){  
  scanf("%s",a[i]);}
  for (i = 1; i < 5; i++){  
   for (j =0; j < 5-i ;j++)  
    if (strcmp(a[j], a[j+1]) > 0){   
     strcpy(t,a[j]);   
     strcpy(a[j],a[j + 1]);   
     strcpy(a[j + 1],t);  
    }
   }
   printf("After sorted:\n");
   for (i = 0; i < 5; i++)
       puts(a[i]);
 return 0;
}

第3个回答  2014-08-11
scanf_s("%s",a[i]);改成scanf_s("%s",a[i], 20);试试追问

真的可以也~可是这是为什么呢?我以前一直都是那么用scanf_s,都没有出问题,可以麻烦你解释一下原因吗?谢谢~

第4个回答  2014-08-11
puts(a[i]); //应该用putchar(a[i]) 吧
相似回答