📜  ctype.h(<cctype> )C / C++中的示例库

📅  最后修改于: 2021-05-25 22:51:33             🧑  作者: Mango

由于字符串.h头文件包含用于处理C / C++中的字符串的内置函数,因此ctype.h / 包含用于分别处理C / C++中的字符的内置函数。

字符有两种类型:

  1. 可打印字符:在终端上显示的字符。
  2. 控制字符:为执行特定操作而启动的字符。

传递给字符函数的参数应为整数类型。如果我们传递字符而不是整数,则将字符类型转换为整数(对应的ASCII值),然后将这些整数作为参数传递。

ctype.h / 头文件下的以下功能适用于普通字符。宽字符函数用于wchar_t类型的字符。

S.No Function Description Return Values
1. isalnum() This function identifies the alphanumeric characters Returns 0 if the passed argument is non – alphanumeric character
Returns non zero value if the passed argument is alphanumeric character
2. isalpha() This function identifies the alphabets from other characters Returns 0 if the passed argument is not an alphabet
Returns non zero value if the passed argument is an alphabet
3. isblank() This function identifies the blank spaces from other characters Returns 0 if the passed argument is not a blank space
Returns nonzero value if the passed argument is a blank space
4. iscntrl() This function identifies the control characters(\n, \b, \t, \r). Returns 0 if the passed argument is not a control character
Returns nonzero value if the passed argument is a control character
5. isdigit() This function identifies numbers in character. Returns 0 if the passed argument is not a number
Returns nonzero value if the passed argument is a number
6. islower() This function identifies the lowercase alphabets. Returns 0 if the passed argument is not a lowercase alphabet
Returns nonzero value if the passed argument is a lowercase alphabet
7. isprint() This function identifies the printable characters. Returns 0 if the passed argument is a non printable character
Returns nonzero value if the passed argument is a printable character
8. ispunct() This function identifies punctuation characters (characters that are neither alphanumeric nor space). Returns 0 if the passed argument is not a punctuation character
Returns nonzero value if the passed argument is a punctuation character
9. isspace() This function identifies white-space characters. Returns 0 if the passed argument is not a white-space character
Returns nonzero value if the passed argument is a white-space character
10. isupper() This function identifies the uppercase alphabets. Returns 0 if the passed argument is not an uppercase alphabet
Returns nonzero value if the passed argument is an uppercase alphabet
11. isxdigit() This function identifies the hexadecimal digit. Returns 0 if the passed argument is not a hexadecimal digit
Returns nonzero value if the passed argument is an hexadecimal digit
12. tolower() This function converts uppercase alphabet to lowercase alphabet. Returns lowercase alphabet of the corresponding uppercase alphabet
13. toupper() This function convert lowercase alphabet to uppercase alphabet. Returns uppercase alphabet of the corresponding lowercase alphabet

以下是实现上述某些功能的示例:

  • 示例1:以下程序标识字母,数字的数量:
    #include 
      
    // Header file containing character functions
    #include 
      
    void identify_alpha_numeric(char a[])
    {
        int count_alpha = 0, count_digit = 0;
        for (int i = 0; a[i] != '\0'; i++) {
            // To check the character is alphabet
            if (isalpha(a[i]))
                count_alpha++;
      
            // To check the character is a digit
            if (isdigit(a[i]))
                count_digit++;
        }
        printf("The number of alphabets are %d\n",
               count_alpha);
        printf("The number of digits are %d",
               count_digit);
    }
      
    int main()
    {
        // String Initialization
        char a[]
            = "Hi 1234, "
              " Welcome to GeeksForGeeks";
        identify_alpha_numeric(a);
    }
    
    输出:
    The number of alphabets are 24
    The number of digits are 4
    
  • 示例2:以下程序标识大写和小写字母的数目,并将大写转换为小写:
    #include 
      
    // Header file containing character functions
    #include 
      
    char* identify_convert_ul(char a[])
    {
        int count_upper = 0, count_lower = 0;
        for (int i = 0; a[i] != '\0'; i++) {
      
            // To check the uppercase characters
            if (isupper(a[i])) {
                count_upper++;
                a[i] = tolower(a[i]);
            }
      
            // To check the lowercase characters
            else if (islower(a[i])) {
                count_lower++;
                a[i] = toupper(a[i]);
            }
        }
        printf("No. of uppercase characters are %d\n",
               count_upper);
        printf("No. of lowercase characters are %d",
               count_lower);
        return a;
    }
      
    int main()
    {
        // String Initialization
        char a[] = "Hi, Welcome to GeeksForGeeks";
        char* p;
        p = identify_convert_ul(a);
        printf("%s", p);
    }
    
    输出:
    No. of uppercase alphabets are 5
    No. of lowercase alphabets are 19
    hI, wELCOME TO gEEKSfORgEEKS
    
  • 示例3:下面的程序在新行中打印每个单词:
    #include 
      
    // Header file containing character functions
    #include 
      
    char* print_word(char a[])
    {
        for (int i = 0; a[i] != '\0'; i++) {
      
            // Space is replaced
            // with control character '\n'
            if (isblank(a[i]))
                a[i] = '\n';
        }
        return a;
    }
    int main()
    {
        // String Initialization
        char a[] = "Hello Everyone."
                   " Welcome to GeeksForGeeks portal. ";
        char* p;
        p = print_word(a);
        printf("%s", p);
    }
    
    输出:
    Hello
    Everyone.
    Welcome
    to
    GeeksForGeeks
    portal.
    
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。