1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c言語_文字列をソートする

Last updated at Posted at 2017-05-02
main.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


typedef int (*fptrOpe)(const char*, const char*);


char* stringToUpper(const char* string){
    char* tmp = (char*)malloc(strlen(string) + 1);
    char* msb = tmp;
    while(*string != 0){
        *(tmp++) = toupper(*(string++));
    }
    *tmp=0;
    return msb;
}


int cmp1(const char* str10, const char* str20){
    char* str12 = stringToUpper(str10);
    char* str22 = stringToUpper(str20);
    int result = strcmp(str12, str22);
    free(str12);
    free(str22);
    return result;
}


int cmp2(const char* str10, const char* str20){
    if(strlen(str10) > strlen(str20)){
        return 1;
    } else if(strlen(str10) == strlen(str20)){
        return 0;
    } else if(strlen(str10) < strlen(str20)){
        return -1;
    }
}


int cmp3(const char* str10, const char* str20){
    if(strlen(str10) < strlen(str20)){
        return 1;
    } else if(strlen(str10) == strlen(str20)){
        return 0;
    } else if(strlen(str10) < strlen(str20)){
        return -1;
    }
}


void sort(char* array[], int size, fptrOpe operation){
    int flag = 1;
    while(flag){
        flag = 0;
        for(int i=0; i<(size-1); i++){
            if(operation(array[i], array[i+1]) > 0){
                flag = 1;
                char* tmp = array[i];
                array[i] = array[i+1];
                array[i+1] = tmp;
            }
        }
    }
}


void disp(char* names[], int size){
    for(int i=0; i<size; i++){
        printf("%s    ", names[i]);
    }
    printf("\n");
}


int main(int argc, const char * argv[]) {

    printf("operation==cmp1\n");
    char* names1[] = {"Apple", "Orange", "Money", "Fish", "Unko"};
    disp(names1, 5);
    sort(names1, 5, cmp1);
    disp(names1, 5);
    printf("\n");

    printf("operation==cmp2\n");
    char* names2[] = {"Apple", "Orange", "Money", "Fish", "Unko"};
    disp(names2, 5);
    sort(names2, 5, cmp2);
    disp(names2, 5);
    printf("\n");

    printf("operation==cmp3\n");
    char* names3[] = {"Apple", "Orange", "Money", "Fish", "Unko"};
    disp(names3, 5);
    sort(names3, 5, cmp3);
    disp(names3, 5);
    printf("\n");

}

実行結果:

main.o
operation==cmp1
Apple    Orange    Money    Fish    Unko    
Apple    Fish    Money    Orange    Unko    

operation==cmp2
Apple    Orange    Money    Fish    Unko    
Fish    Unko    Apple    Money    Orange    

operation==cmp3
Apple    Orange    Money    Fish    Unko    
Orange    Apple    Money    Fish    Unko 

入力文字列namesを関数stringToUpperで大文字にしてから
順番を入れ替えるというサンプルコードです。
文字列の入れ替えは、関数ポインタfptrOpeの指す関数cmpの機能に基づいて行われます。

関数cmp1, cmp2, cmp3のどれを使っても、文字列"Unko"が先頭にくることはありません。
ソートって難しい、、

参考:

詳説Cポインタ

 
 
 
 
 

どやっ!

1
2
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?