Use of the Cctype header file (character processing library)

Source: Internet
Author: User

Use of Cctype header files in C + +

The header file Cctype (character processing library) defines the library function for character judgment and processing, including the header file before use:

#include <cctype>

using namespace Std;

The list of commonly used functions in the Cctype header file is as follows:

Function name return value
Isalnum () If the argument is an alphanumeric, that is, a letter or a number, the function returns True
Isalpha () If the argument is a letter, the function returns True
Iscntrl () If the argument is a control character, the function returns True
IsDigit () If the argument is a number (0-9), the function returns True
Isgraph () If the argument is a print character other than a space, the function returns True
Islower () If the argument is a lowercase letter, the function returns True
Isprint () If the argument is a print character (including a space), the function returns True
ISPUNCT () If the argument is a punctuation mark, the function returns True
Isspace () If the parameter is a standard white space character, such as a space, line break, horizontal, or vertical tab, the function returns True
Isupper () If the argument is uppercase, the function returns True
Isxdigit () If the argument is a hexadecimal number, which is 0-9, A-f, a-f, the function returns True

ToLower () If the argument is uppercase, returns its lowercase, otherwise returns the argument
ToUpper () If the argument is lowercase, return it in uppercase, or return the argument

Common function functions in the Cctype header file are divided into the following two categories:

Function One: Character test
1> function prototypes are int isxxxx (int)
The 2> parameter is int, and any arguments are promoted to integral type
3> only correctly handles values between [0, 127]

If a character is uppercase, use Isupper () if the argument is uppercase, the function returns True, and the function returns True, such as isalnum (), if the argument is an alphanumeric character, that is, a letter or a number. Let's look at the output with a small example:

cout << "IIS:" << isupper (' B ') << "\ n";

Output: I is:0

If the parameter is changed to ' B ', it will be output: I is:1

Function two: Character mapping
1> function prototype is int toxxxx (int)
2> parameter detection, if the scope of the conversion, otherwise unchanged

Inttolower (int); ' A ' ~ ' Z ' ==> ' a ' ~ ' Z '

int toupper (int); ' A ' ~ ' Z ' ==> ' a ' ~ ' Z '

The following program uses the loop to determine the output ASCII from the character type between 0~127

//use loops to determine the type of characters between ASCII from 0~127/*Common function function name in Cctype return value isalnum () If the argument is an alphanumeric, that is, a letter or a number, the function returns Trueisalpha () if the argument is a letter, the function returns Trueiscntrl () if the argument is a control character,  The function returns Trueisdigit () if the argument is a number (0-9), the function returns Trueisgraph () if the argument is a print character other than a space, the function returns Trueislower () if the argument is lowercase, the function returns Trueisprint ()  If the argument is a print character (including a space), the function returns TRUEISPUNCT () if the argument is a punctuation mark, the function returns Trueisspace () if the parameter is a standard white space character, such as a space, line break, horizontal or vertical tab, the function returns Trueisupper ()  If the argument is uppercase, the function returns Trueisxdigit () if the argument is a hexadecimal number, which is 0-9, A-f, a-f, the function returns Truetolower () if the argument is uppercase, returns its lowercase, otherwise returns the argument ToUpper () If the argument is lowercase, return it in uppercase, or return the argument*/#include<iostream>#include<cctype>using namespacestd;intMain () {unsignedCharC=' /';  for(;(int) c<=127; C + +) {cout<<"ASCII is"<< (int) c<<"the character is"; if(Iscntrl (c))//determines whether the control character is{cout<< (Iscntrl (c)?"control characters":""); cout<< (Isspace (c)?"standard white space characters, such as spaces, line breaks, horizontal or vertical tabs":""); }        Else  //non-control characters{cout<<c<<" "; cout<< (Isprint (c)?"print characters (including spaces)":""); cout<< (Isgraph (c)?"print characters other than spaces":""); cout<< (Isalnum (c)?"Alpha-Numeric":""); cout<< (Isalpha (c)?"Letters":""); cout<< (Islower (c)?"Lowercase Letters":""); cout<< (Isupper (c)?"Uppercase Letters":""); cout<< (IsDigit (c)?"number (0-9)":""); cout<< (Isxdigit (c)?"hexadecimal digits (0-9,a-f,a-f)":""); cout<< (Ispunct (c)?"Punctuation":""); } cout<<Endl; //cout<< "=============================================" <<endl;    }    return 0;}

Run results

Use of the Cctype header file (character processing library)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.