strcoll

文字列の比較をその時点のロケール (locale) に従って行います.

#include <string.h>
int strcoll(
    const char *s1,
    const char *s2
);

strcoll 関数は s1 が指す文字列と s2 が指す文字列を比較します.文字列の比較はロケール情報の LC_COLLATE カテゴリに基づいて行われます.

戻り値

  • s1 = s2のとき: 0
  • s1 > s2のとき: 正の整数
  • s1 < s2のとき: 負の整数

C言語サンプルプログラム

strcoll 関数を使用して文字列を比較するサンプルプログラムを以下に示します.

/* header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

/* main */
int main(void) {
    char s1[] = "12345";
    char s2[] = "12367";
    int result;

    /* ロケールを日本にセット */
    setlocale(LC_ALL, "JPN");

    result = strcoll(s1, s2);
    if ( result == 0 ) {
        printf("s1とs2は同じ文字列です.\n");
    } else if ( result > 0 ) {
        printf("s1のほうが大きい文字列です.\n");
    } else {
        printf("s2のほうが大きい文字列です.\n");
    }

    return EXIT_SUCCESS;
}

実行例

サンプルプログラムの実行結果は以下のようになります.

s2のほうが大きい文字列です.