用指向指針的指針的方法對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