C语言问题 。 请教 高手 !

[复制链接]
查看11 | 回复1 | 2011-5-18 11:23:16 | 显示全部楼层 |阅读模式
字符串排序
在主函数中输入10个不等长的字符串放入二维字符数组中,编写函数sort()利用指针数组对其排序,在主函数中输出排好序的字符串。
求解思路:仿照课堂例题程序。定义二维字符数组的原因是不能使用字符指针数组直接接收输入的字符串,需要先开辟存储空间。
回复

使用道具 举报

千问 | 2011-5-18 11:23:16 | 显示全部楼层
#includestdio.h
#includestring.h
void
sort(charp[10][100])
{
        char*ptr[10];
        inti,j;
        chararray[100];
        for(i=0;i10;i)
                ptr=p;
        for(i=0;i9;i)
                for(j=i1;j10;j)
                        if(strlen(ptr)strlen(ptr[j])){
                                strcpy(array,ptr);
                                strcpy(ptr,ptr[j]);
                                strcpy(ptr[j],array);
                        }
}
int
main()
{
        charstr[10][100];
        inti;
        printf(\"请输入10个不等长的字符串:\\n\");
        for(i=0;i10;i)
        scanf(\"%s\",str);
        printf(\"排序后的字符串为:\\n\");
        sort(str);
        for(i=0;i10;i)
                printf(\"%s\\n\",str);
        return0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行