开发者

isLetter with accented characters in C

I'd like to create (or find) a C function to check if a char c is a letter... I can do this for a-z and A-Z easily of course.

However i get an error if testing c == á,ã,ô,ç,ë, etc

Probably those special characters are stored in more then a char...

I'd like to know: How these special characters are stored, which arguments my function needs to receive, and how to do it? I'd also like to know if are there any standard func开发者_如何学JAVAtion that already does this.


I think you're looking for the iswalpha() routine:

   #include <wctype.h>

   int iswalpha(wint_t wc);

DESCRIPTION
   The iswalpha() function is the wide-character equivalent of
   the isalpha(3) function.  It tests whether wc is a wide
   character belonging to the wide-character class "alpha".

It does depend upon the LC_CTYPE of the current locale(7), so its use in a program that is supposed to handle multiple types of input correctly simultaneously might not be ideal.


If you are working with single-byte codesets such as ISO 8859-1 or 8859-15 (or any of the other 8859-x codesets), then the isalpha() function will do the job if you also remember to use setlocale(LC_ALL, ""); (or some other suitable invocation of setlocale()) in your program. Without this, the program runs in the C locale, which only classifies the ASCII characters (8859-x characters in the range 0x00..0x7F).

If you are working with multibyte or wide character codesets (such as UTF8 or UTF16), then you need to look to the wide character functions found in <wchar.h> and <wctype.h>.


How these characters are stored is locale-dependent. On most UNIX systems, they'll be stored as UTF8, whereas a Win32 machine will likely represent them as UTF16. UTF8 is stored as a variable-amount of chars, whereas UTF16 is stored using surrogate pairs - and thus inside a wchar_t (or unsigned short) (though incidentally, sizeof(wchar_t) on Windows is only 2 (vs 4 on *nix), and thus you'll often need 2 wchar_t types to store the 1 character if a surrogate pair encoding is used - which it will be in many cases).

As was mentioned, the iswalpha() routine will do this for you, and is documented here. It should take care of locale-specific issues for you.


You probably want http://site.icu-project.org/. It provides a portable library with APIs for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜