What does ctype h actually define


20.2 - character classification and conversion

The header file contains functions with which individual characters can be checked or converted. The header file , however, contains the declarations of the counterparts for wide characters. In table 20.1 you will find the syntax of the individual functions and the descriptions:


Function in Function in description

Tests whether a character is an alphanumeric character.

Tests whether a character is a letter.

Tests whether a character is a control character (e.g. (Ctrl) + (B)).

Tests whether a character is a decimal digit.

Tests whether a character is a printable character.

Tests whether a character is a lowercase letter.

Tests whether a character is a printable character, but not a space.

Tests whether a character is a punctuation mark.

Tests whether a character is a space (space character).

Tests whether a character is an uppercase letter.

int iswxdigit (wint_t wc);

Tests whether a character is a hexadecimal digit.

The isascii macro checks whether the character c is a standardized character from the ASCII character set (0–127). If not, it can be converted with toascii.

If c is a space, 1 is returned, otherwise 0 (only available with ANSI-C99 compilers).

Converts uppercase to lowercase letters.

Converts lowercase to uppercase.

toascii is a macro that limits the transferred value c (by deleting all bits except the 7 least significant) to the range 0 to 127 and returns the result of this operation. However, this means that national characters such as 'ä', 'ü', 'ö' ... are displayed incorrectly.


All functions in the table expect an int or wint_t argument, the value of which must be representable as EOF (or WEOF) or unsigned char or wchar_t. If successful, these functions return a value other than 0. If an error occurs, the return value is always 0.


Note

Apart from the isdigit () and iswdigit () functions, the result depends on the setting of the local environment for the LC_CTYPE category. You can query and change this environment with setlocale () (see Section 20.5, » - country-specific peculiarities«).


The header file also contains two expandable functions for checking wide characters. The syntax of these functions is:

int iswctype (wint_t wc, wctype_t desc); wctype_t wctype (const char * property);

These functions allow you to perform at least the same tests as in Table 20.1. All you have to do is leave out the isw prefix for property. Thus, a check for a wide character wc is equivalent to the tests in the comment after it:

iswctype (wc, wctype ("alnum")) // iswalnum (wc) iswctype (wc, wctype ("alpha")) // iswalpha (wc) iswctype (wc, wctype ("blank")) // iswblank (wc ) iswctype (wc, wctype ("cntrl")) // iswcntrl (wc) iswctype (wc, wctype ("digit")) // iswdigit (wc) iswctype (wc, wctype ("graph")) // iswgraph ( wc) iswctype (wc, wctype ("lower")) // iswlower (wc) iswctype (wc, wctype ("print")) // iswprint (wc) iswctype (wc, wctype ("punct")) // iswpunct (wc) iswctype (wc, wctype ("space")) // iswspace (wc) iswctype (wc, wctype ("upper")) // iswupper (wc) iswctype (wc, wctype ("xdigit")) // iswxdigit (wc)

The advantage of this implementation is obvious. It is easy to define additional strings for property that are required for certain locale-specific characters.

Just as there are functions for classifying wide characters, there are also functions for converting wide characters. The syntax of these functions is:

wint_t towctrans (wint_t wc, wctrans_t desc); wctrans_t wctrans (const char * property);

You can use the strings tolower and toupper for property. Thus, a conversion for a wide character wc is equivalent to the conversion in the comment after it:

towctrans (wc, wctrans ("tolower")) // towlower (wc) towctrans (wc, wctrans ("toupper")) // towupper (wc)

A listing that uses normal byte-oriented characters now follows to demonstrate the individual functions:

/ * big_small.c * / #include #include #include #include / * prototypes * / void upper case (char *, char *); void lower case (char *, char *); void international (char *, char *); void open_files (char *, char *, FILE **, FILE **); void upper case (char * arg2, char * arg3) {FILE * in, * out; int c; open_files (arg2, arg3, & in, & out); while ((c = getc (in))! = EOF) {if (islower (c)) putc (toupper (c), out); else putc (c, out); }} void lower case (char * arg2, char * arg3) {FILE * in, * out; int c; open_files (arg2, arg3, & in, & out); while ((c = getc (in))! = EOF) {if (isupper (c)) putc (tolower (c), out); else putc (c, out); }} void international (char * arg2, char * arg3) {FILE * in, * out; int c; open_files (arg2, arg3, & in, & out); while ((c = getc (in))! = EOF) {if (isascii (c) == 0) putc (toascii (c), out); else putc (c, out); }} void open_files (char * source, char * target, FILE ** input, FILE ** output) {if ((* input = fopen (source, "r +")) == NULL) {printf ("Error opening (Read) from% s \ n ", source); exit (EXIT_FAILURE); } if ((* output = fopen (target, "w +")) == NULL) {printf ("Error opening (writing)% s \ n", target); exit (EXIT_FAILURE); }} int main (int argc, char * argv []) {if (argc <4) {printf ("Usage:" "Program name - [switch] file.txt target file.txt \ n" "for - [switch]: -b (complete text file in upper case) \ n "" -s (complete text file in lower case) \ n "" -i (national characters to ASCII characters \ n "); return EXIT_FAILURE;} if (strcmp (argv [1] , "- b") == 0) upper case (argv [2], argv [3]); else if (strcmp (argv [1], "- s") == 0) lower case (argv [2], argv [3]); else if (strcmp (argv [1], "- i") == 0) international (argv [2], argv [3]); else {printf ("usage:" "program name - [switch ] file.txtZieldatei.txt \ n "" for - [switch]: -b (complete text file in upper case) \ n "" -s (complete text file in lower case) \ n "" -i (national characters to ASCII characters \ n "); return EXIT_SUCCESS;} return EXIT_SUCCESS;}

You can use this code to convert any letter in a text file to an upper, lower case, or ASCII character.



your opinion

How did you like the Openbook? We always look forward to your feedback. Please send us your feedback as an e-mail to [email protected]