调用strlen函数对字符串数组进行排序并放入W数组中

来源:百度知道 编辑:UC知道 时间:2024/06/22 08:33:43
#include "stdio.h"
#include "string.h"
main()
{char a[5][10], w[5][10];
int i , j , t ;
for(i=0;i<5;i++)
gets(a[i]);
for(i=4;i>=0;i++)
for(j=0;j<=i;j++)
if(i=0); strcpy(w[i],a[i]);
if(strlen(a[j])>strlen(a[j+1]))};
strcpy(t,a[j+1]); strcpy(a[j+i],a[j]); strcpy(a[j],t); strcpy(w[i],a[j+i]);
for(i=0;i<4;i++)
puts(w[i]);
}
这道程序有问题吗?请高人指教

问题太多,没法解释.你还是直接看我的代码吧

#include "stdio.h"
#include "string.h"
main()
{char a[5][10], w[5][10];
char t[10];
int i , j ;
for(i=0;i<5;i++)
gets(a[i]);
for(i=4;i>0;i--)
for(j=0;j<i;j++)
if(strlen(a[j])>strlen(a[j+1]))
{strcpy(t,a[j+1]); strcpy(a[j+1],a[j]); strcpy(a[j],t); }
for(i=0;i<5;i++)
{
strcpy(w[i],a[i]);
puts(w[i]);
}
}

总之,你的错误有:
1.t的类型错了
2.有一个1写成i了
3.if语句后面直接加了分号
4.部分strcpy(w[i],a[i]);的代码完全多余
5.for(i=4;i>=0;i++)应该是i--

if(i=0); 后面的分号要去掉

你这是按字符串的长度排序?
1 #include "stdio.h"
2 #include "string.h"
3 main()
4 {char a[5][10], w[5][10];
5 int i , j , t ;
6 for(i=0;i<5;i++)
7 gets(a[i]);
8 for(i=4;i>=0;i++)
9 for(j=0;j<=i;j++)
10 if(i=0); strcpy(w[i],a[i]);
11 if(strlen(a[j])>strlen(a[j+1]))};
12 strcpy(t,a[j+