wctype関数の詳細ガイド:ワイド文字の世界を深く掘り下げる
wctype 関数のプロトタイプは次のとおりです。
#include <wctype.h>
wctype_t wctype(const char *property);
この関数は、property
パラメータとして渡される文字列に基づいて、ワイド文字カテゴリを表す wctype_t
型の値を返します。property
文字列は、次のいずれかのカテゴリを指定する必要があります。
"xdigit"
:16進数"upper"
:大文字"space"
:空白"punct"
:句読点"print"
:印刷可能な文字"lower"
:小文字"graph"
:グラフィック文字"digit"
:数字"cntrl"
:制御文字"blank"
:空白"alpha"
:英字"alnum"
:英数字
wctype 関数の例を次に示します。
#include <stdio.h>
#include <wctype.h>
int main() {
wchar_t str[] = L"This is a wide string.";
wchar_t c;
for (int i = 0; str[i] != L'\0'; ++i) {
c = str[i];
if (iswctype(c, wctype("alnum"))) {
printf("%c is an alphanumeric character.\n", c);
} else if (iswctype(c, wctype("space"))) {
printf("%c is a whitespace character.\n", c);
} else if (iswctype(c, wctype("punct"))) {
printf("%c is a punctuation character.\n", c);
} else {
printf("%c is not an alphanumeric, whitespace, or punctuation character.\n", c);
}
}
return 0;
}
このプログラムは、ワイド文字列 str
をループし、各文字を wctype
関数を使用して分類します。文字が英数字、空白、句読点のいずれかに属する場合は、そのカテゴリがコンソールに印刷されます。
wctype 関数は、ワイド文字列を処理するプログラムで役立ちます。たとえば、この関数を使用して、文字列内の英字のみを抽出したり、空白を除去したり、句読点を置換したりすることができます。
wctype
関数は、現在のロケールの LC_CTYPE カテゴリに基づいてワイド文字を分類します。ロケールを変更するには、setlocale
関数を使用する必要があります。wctype
関数は、ワイド文字のみをサポートします。バイト文字を分類するには、isctype
関数を使用する必要があります。
文字列中の英字のみを抽出する
このプログラムは、ワイド文字列 str
をループし、各文字が英字かどうかを wctype
関数を使用して判断します。英字の場合は、その文字をコンソールに印刷します。
#include <stdio.h>
#include <wctype.h>
int main() {
wchar_t str[] = L"This is a wide string with numbers and punctuation.";
wchar_t c;
for (int i = 0; str[i] != L'\0'; ++i) {
c = str[i];
if (iswctype(c, wctype("alpha"))) {
printf("%c", c);
}
}
return 0;
}
このプログラムを実行すると、次の出力が表示されます。
Thisisawidestringwithnumbersandpunctuation
空白を除去した文字列を出力する
このプログラムは、ワイド文字列 str
をループし、各文字が空白かどうかを wctype
関数を使用して判断します。空白ではない場合は、その文字を new_str
文字列に追加します。最後に、new_str
文字列をコンソールに印刷します。
#include <stdio.h>
#include <wctype.h>
int main() {
wchar_t str[] = L" This is a wide string with many spaces. ";
wchar_t new_str[100];
int i, j;
i = 0;
j = 0;
while (str[i] != L'\0') {
if (!iswctype(str[i], wctype("space"))) {
new_str[j] = str[i];
++j;
}
++i;
}
new_str[j] = L'\0';
printf("%s\n", new_str);
return 0;
}
Thisisawidestringwithmanyspaces.
句読点をすべてピリオドに置き換える
このプログラムは、ワイド文字列 str
をループし、各文字が句読点かどうかを wctype
関数を使用して判断します。句読点の場合は、その文字を .
に置き換えます。最後に、置き換え後の文字列をコンソールに印刷します。
#include <stdio.h>
#include <wctype.h>
int main() {
wchar_t str[] = L"This is a wide string, isn't it? Yes, it is!";
wchar_t c;
for (int i = 0; str[i] != L'\0'; ++i) {
c = str[i];
if (iswctype(c, wctype("punct"))) {
str[i] = L'.';
}
}
printf("%s\n", str);
return 0;
}
This is a wide string. isn't it? Yes. it is!
iswctype 関数
iswctype
関数は、wctype
関数とほぼ同じですが、ワイド文字ではなくバイト文字を分類するために使用されます。ワイド文字列を処理する場合は、iswctype
関数は使用できません。
文字クラス関数
C言語には、isalnum
、isalpha
、isspace
、isdigit
、ispunct
などの文字クラス関数があります。これらの関数は、それぞれ英数字、英字、空白、数字、句読点かどうかを判断するために使用できます。これらの関数は、wctype
関数よりもシンプルで使いやすい場合があります。
カスタム判定ロジック
必要な判定が単純な場合は、カスタム判定ロジックを使用することもできます。たとえば、英字のみを抽出したい場合は、次のコードのように isupper
と islower
関数を使用してループできます。
#include <stdio.h>
int main() {
wchar_t str[] = L"This is a wide string.";
wchar_t c;
for (int i = 0; str[i] != L'\0'; ++i) {
c = str[i];
if (isupper(c) || islower(c)) {
printf("%c", c);
}
}
return 0;
}
正規表現
正規表現を使用して、ワイド文字列をパターンと照合することもできます。これは、より複雑な判定が必要な場合に役立ちます。
- 機能: 必要な判定が複雑な場合は、カスタム判定ロジックや正規表現が必要になる場合があります。
- パフォーマンス: カスタム判定ロジックや正規表現は、
wctype
関数よりもパフォーマンスが低下する可能性があります。 - シンプルさ:
iswctype
関数や文字クラス関数は、wctype
関数よりもシンプルで使いやすい場合があります。