📜  C++中的字符分类:cctype

📅  最后修改于: 2021-05-30 13:21:49             🧑  作者: Mango

使用函数库中指定的函数,可以在C++中进行字符分类。本文讨论了许多用于对字符进行分类的函数。

1. isalpha():如果字符是字母,则此函数返回true,否则返回false。根据此函数, az和AZ中的所有字符返回true。

2. isalnum():如果字符是字母或数字,则此函数返回true,否则返回false。根据此函数来自az,AZ的所有字符以及所有数字均返回true。

3. isdigit():如果字符是数字,则此函数返回true,否则返回false。根据此函数,所有数字均返回true。

// C++ program to demonstrate insalpha(), isnum() 
// and isdigit()
#include 
#include 
using namespace std;
  
int main()
{
    // initializing character array
    char ch[5] = "g1";
  
    // checking for isalpha() function
    for (int i=0; i<2; i++)
    {
        if (isalpha(ch[i]))
            cout << ch[i] << " is alphabet" << endl;
        else
            cout << ch[i] << " is not alphabet" << endl;
    }
  
    cout << endl;
  
    // checking for isalnum() function
    for (int i=0; i<2; i++)
    {
        if (isalnum(ch[i]))
            cout << ch[i] << " is alphanumeric" << endl;
        else
            cout << ch[i]  << " is not alphanumeric" << endl;
    }
  
    cout << endl;
  
    // checking for isdigit() function
    for (int i=0; i<2; i++)
    {
        if (isdigit(ch[i]))
            cout << ch[i]  <<  " is digit" << endl;
        else
            cout << ch[i] << " is not digit" << endl;
    }
}

输出 :

g is alphabet
1 is not alphabet

g is alphanumeric
1 is alphanumeric

g is not digit
1 is digit

4. isblank():如果字符是空格或制表符,则此函数返回true,否则返回false。

5. isspace():如果字符是空格或制表符或空白控制代码(例如\ n,\ r),则此函数返回true,否则返回false。

6. iscntrl():如果字符是制表符或任何控制代码,则此函数返回true,否则返回false。

// C++ program to demonstrate iscntrl(), isblank() and
// isspace()
#include 
#include 
using namespace std;
  
int main()
{
    // initializing character array
    char ch[4] = " \n\t";
  
    // checking for iscntrl() function
    for (int i=0; i<3; i++)
    {
        if (iscntrl(ch[i]))
            cout << " Character is control code " << endl;
        else
            cout << " Character is not control code" << endl;
    }
  
    cout << endl;
  
    // checking for isblank() function
    for (int i=0; i<3; i++)
    {
        if (isblank(ch[i]))
            cout << " Character is blank" << endl;
        else
            cout << " Character is not blank" << endl;
    }
  
    cout << endl;
  
    // checking for isspace() function
    for (int i=0; i<3; i++)
    {
        if (isspace(ch[i]))
            cout << " Character is space" << endl;
        else
            cout << " Character is not space" << endl;
    }
}

输出 :

Character is not control code
 Character is control code 
 Character is control code 

 Character is blank
 Character is not blank
 Character is blank

 Character is space
 Character is space
 Character is space

7. isprint():如果字符可以在控制台上打印,则此函数返回true,即除控制代码外的所有其他字符返回false。

8. isxdigit():如果字符为十六进制(即0-9),则此函数返回true,否则返回false。

9. ispunct():如果字符是标点符号,则此函数返回true,否则返回false。

// C++ program to demonstrate isprint(), isxdigit() and
// ispunct()
#include 
#include 
using namespace std;
  
int main()
{
    // initializing character array
    char ch[6] = "\t@gf1";
  
    // checking for isprint() function
    for (int i=0; i<5; i++)
    {
        if (isprint(ch[i]))
            cout << ch[i] << " is printable character " << endl;
        else
            cout << ch[i] << " is not printable Character" << endl;
    }
  
    cout << endl;
  
    // checking for isxdigit() function
    for (int i=0; i<5; i++)
    {
        if (isxdigit(ch[i]))
            cout << ch[i] << " is hexadecimal Character" << endl;
        else
            cout << ch[i] << " is not hexadecimal Character" << endl;
    }
  
    cout << endl;
  
    // checking for ispunct() function
    for (int i=0; i<5; i++)
    {
        if (ispunct(ch[i]))
            cout << ch[i] << "  is punctuation mark" << endl;
        else
            cout << ch[i] <<  "  is not punctuation mark" << endl;
    }
}

输出 :

is not printable Character
@ is printable character 
g is printable character 
f is printable character 
1 is printable character 

     is not hexadecimal Character
@ is not hexadecimal Character
g is not hexadecimal Character
f is hexadecimal Character
1 is hexadecimal Character

      is not punctuation mark
@  is punctuation mark
g  is not punctuation mark
f  is not punctuation mark
1  is not punctuation mark

参考:http://www.cplusplus.com/reference/cctype/

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”