用指向指针的指针的方法对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;
            }
        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM