用指向指针的指针的方法对5个字符串排序并输出。

 

#include <stdio.h>
#include <string.h>
//用指向指针的指针的方法对5个字符串排序并输出。
int main(){
    void sort(char * pstr[5]);
    int i;
    char str[5][50]={"asdfadsf","oiuyoiuy","lkwjerht","uytr","qoiurty"};
    char * pstr[5];
    printf("排序前:\n");
    for(i=0; i<5; i++){
        pstr[i]=str[i];
        printf("%s\n",pstr[i]);
    }
    //对字符串排序
    sort(pstr);
    printf("排序后:\n");
    for(i=0; i<5; i++){
        printf("%s\n",pstr[i]);
    }
    return 0;
}

void sort(char * pstr[5]){
    char * t;
    int i,j;
    for(i=0; i<5; i++){
        for(j=i+1; j<5; j++){
            if(strcmp(pstr[i],pstr[j]) > 0)    
            {    t=pstr[j];
                pstr[j]=pstr[i];
                pstr[i]=t;
            }
        }
    }
}

 

posted @ 2017-08-04 16:08  Allen101  阅读(2723)  评论(0编辑  收藏  举报