C言語プログラミングの奥深さを探求:iswdigit() 関数でワイド文字列の数字を判定
#include <stdio.h>
#include <cwctype.h>
int main() {
wchar_t str[] = L"This is a string with digits: 123456789";
int i;
for (i = 0; str[i] != L'\0'; ++i) {
if (iswdigit(str[i])) {
printf("The character '%lc' is a digit.\n", str[i]);
}
}
return 0;
}
This code will print the following output:
The character '1' is a digit.
The character '2' is a digit.
The character '3' is a digit.
The character '4' is a digit.
The character '5' is a digit.
The character '6' is a digit.
The character '7' is a digit.
The character '8' is a digit.
The character '9' is a digit.
The iswdigit()
function is useful for a variety of tasks, such as parsing numbers from strings and validating user input. It is also a key component of many character classification functions, such as iswalnum()
and iswspace()
.
Here are some additional things to keep in mind about the iswdigit()
function:
- The
iswdigit()
function is a macro in some implementations of C. This means that it may be expanded in-place, which can make it more efficient than a function call. - The
iswdigit()
function is locale-independent. This means that it will always return the same result for a given wide character, regardless of the current locale. - The
iswdigit()
function is only defined for wide characters. If you need to check whether a narrow character is a digit, you can use theisdigit()
function from the<ctype.h>
header file.
Checking if a string contains only digits
#include <stdio.h>
#include <cwctype.h>
int main() {
wchar_t str[] = L"123456789";
int i;
int is_all_digits = 1;
for (i = 0; str[i] != L'\0'; ++i) {
if (!iswdigit(str[i])) {
is_all_digits = 0;
break;
}
}
if (is_all_digits) {
printf("The string '%ls' contains only digits.\n", str);
} else {
printf("The string '%ls' does not contain only digits.\n", str);
}
return 0;
}
The string '123456789' contains only digits.
Extracting digits from a string
#include <stdio.h>
#include <cwctype.h>
int main() {
wchar_t str[] = L"This string has digits: 123abc456";
wchar_t digits[10];
int i, digit_index = 0;
for (i = 0; str[i] != L'\0'; ++i) {
if (iswdigit(str[i])) {
digits[digit_index++] = str[i];
}
}
digits[digit_index] = L'\0';
printf("The extracted digits are: %ls\n", digits);
return 0;
}
The extracted digits are: 123456
#include <stdio.h>
#include <cwctype.h>
#include <stdlib.h>
int main() {
wchar_t str[] = L"12345";
int number = 0;
int i;
for (i = 0; str[i] != L'\0'; ++i) {
if (iswdigit(str[i])) {
number = number * 10 + (str[i] - L'0');
}
}
printf("The number converted from the string is: %d\n", number);
return 0;
}
The number converted from the string is: 12345
Using the isdigit() function with wide character literals
The isdigit()
function from the <ctype.h>
header file can be used to check whether a narrow character is a digit. However, it can also be used to check wide characters by converting the wide character to a narrow character using a wide character literal. For example, the following code snippet is equivalent to calling iswdigit(L'0')
:
if (isdigit(L'0')) {
// ...
}
This approach is only valid for wide character literals, which are written using the L
prefix. It cannot be used with wide character variables or expressions.
Manually checking the character code
Each wide character in C has a corresponding Unicode code point. The decimal digits have code points between U+0030 (0) and U+0039 (9). Therefore, you can check whether a wide character is a digit by comparing its code point to these values. For example, the following code snippet is equivalent to calling iswdigit(c)
:
wchar_t c = L'1';
if (c >= L'0' && c <= L'9') {
// ...
}
This approach is more verbose than using the iswdigit()
function, but it does not require any additional header files.
Using a custom character classification function
You can write your own custom character classification function to check whether a wide character is a digit. This can be useful if you need to extend the functionality of the iswdigit()
function or if you want to avoid using the ctype.h
header file. For example, the following code snippet defines a custom function my_iswdigit()
that is equivalent to the iswdigit()
function:
#include <stdio.h>
int my_iswdigit(wchar_t c) {
return c >= L'0' && c <= L'9';
}
int main() {
wchar_t c = L'1';
if (my_iswdigit(c)) {
printf("The character '%lc' is a digit.\n", c);
} else {
printf("The character '%lc' is not a digit.\n", c);
}
return 0;
}
This approach gives you the most control over how the character classification is performed, but it also requires the most code.
In general, the iswdigit()
function is the most concise and convenient way to check whether a wide character is a digit. However, the other alternatives described above may be useful in certain situations.
Here is a table summarizing the pros and cons of each approach:
Approach | Pros | Cons |
---|---|---|
iswdigit() | Concise, convenient | Requires cwctype.h header file |
isdigit() with wide character literals | Does not require cwctype.h header file | Only works with wide character literals |
Manual code point check | No header files required, most control over classification | Verbose |
Custom character classification function | Most control over classification | Requires most code |