Chapter 9. Internal Processing and File I/O
79
#include
#include
int main(int argc, char **argv)
{
int len, n;
wchar_t *wp;
/* All softwares using locale should write this line */
setlocale(LC_ALL, "");
if (argc < 2) {
printf("Usage: %s [string]\n", argv[0]);
return 0;
}
printf("Your string is: \"%s\".\n", argv[1]);
/* The concept of 'byte' is universal. */
len = strlen(argv[1]);
printf("Length of your string is: %d bytes.\n", len);
/* To obtain number of characters, it is the easiest way */
/* to convert the string into wide string.
The number of */
/* characters is equal to the number of wide characters. */
/* It does not exceed the number of bytes. */
n = strlen(argv[1]) * sizeof(wchar_t);
wp = (wchar_t *)malloc(n);
len = mbstowcs(wp, argv[1], n);
printf("Length of your string is: %d characters.\n", len);
printf("Width of your string is: %d columns.\n", wcswidth(wp, len));
return 0;
}
This program can count multibyte characters correctly. Of course the user has to set LANG vari
able properly.
For example, on UTF 8 xterm. . .
$ export LANG=ko_KR.UTF 8
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved