c语言 字符串排序

问题:

题目描述

用指向指针的指针的方法对5个字符串排序并输出。要求将排序单独写成一个函数。字符串和n在主函数中输入。最后在主函数中输出。

输入

n和n个字符串

输出

排序后的字符串

样例输入

5
12345
123
abce
abcde
abcd

样例输出

123
12345
abcd
abcde
abce

提示

/* C代码 */

int main()

{

    void sort(char **,int );

    int i,n;

    char **p,*pstr[20],str[20][80];

    scanf("%d",&n);

    for (i=0; i
        pstr[i]=str[i];

    for (i=0; i
        scanf("%s",pstr[i]);

    p=pstr;

    sort(p,n);

    for (i=0; i
        printf("%s\n",pstr[i]);

    return 0;

}



/* C++代码 */

int main()

{

    void sort(char **,int );

    int i,n;

    char **p,*pstr[20],str[20][80];

cin>>n;

    for (i=0; i
        pstr[i]=str[i];

    for (i=0; i
        cin>>pstr[i];

    p=pstr;

    sort(p,n);

    for (i=0; i
        cout<
    return 0;

}

#include
#include
int sort(char **p,int n)
{
    int i,j;
    char t[80];
    for(i=0;i0)
            {
                strcpy(t,p[i]);
                strcpy(p[i],p[j]);
                strcpy(p[j],t);
            }
        }
    }
    return 0;
}
int main()
{
    void sort(char **,int );
    int i,n;
    char **p,*pstr[20],str[20][80];
    scanf("%d",&n);
    for (i=0; i

总结:

包含的函数中,strcmp用来比较,大于零就是前面的大。strcpy用来复制函数,后一个复制到前一个上面。

你可能感兴趣的:(c语言)