1
我們必須輸入n個字符串,並按照長度和ascii值進行排序的升序排序。 請幫助.... 有一個樣本測試案例: 沒有琴絃5 奧馬爾 蘋果 香蕉 螞蟻 貓使用ascii和字符串長度對字符串進行排序
,輸出必須是: 螞蟻貓奧馬爾蘋果香蕉
enter code here:
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,l,m,j;
char str[20][20],temp[20],temp2[20][20];
printf("enter no of strings\n");
scanf("%d",&n);
// string is given as input
for(i=0; i<n; i++)
{
fflush(stdin);
scanf("%s",str[i]);
l=strlen(str[i]);
printf("len is %d\n",l);
}
//sorting on the basis of length
for(i=0; i<=n; i++)
for(j=i+1; j<=n; j++)
{
if(strlen(str[i])>strlen(str[j]))
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
//sorting on the basis of ascii values
for(i=0; i<n; i++)
{
if(l==m)// spcl condition
{
printf("\ncheck",l,m);
for(j=0; j<strlen(str[i]); j++)
{
if(strcmp(str[j][i],str[j][++i])>0)
{
strcpy(temp2[20],str[i]);
strcpy(str[i],str[++i]);
strcpy(str[++i],temp2[20]);
}
}
}
else// default condition
{
strcpy(temp2[20],str[i]);
strcpy(str[i],str[++i]);
strcpy(str[++i],temp2[20]);
}
}
//display each string
printf("the strings are\n");
for(i=0; i<n; i++)
{
printf("%s",str[i]);
printf("\n");
}
return 0;
}