• 学习笔记二十三——字符函数库cctype【转】


    本文转载自:

    字符函数库cctype

    在头文件cctype(ctype.h)中定义了一些函数原型,可以简化输入确定字符是否为大写字母、数字、标点符号等工作。 
    例如: 
    如果ch是一个字母,则isalpha(ch)函数返回一个非零值,否则返回0; 
    如果ch是一个标点符号,则ispunct(ch)函数返回非零值,否则返回0; 
    (这些函数的返回类型为int,而不是bool,但通常bool转换让我们能够将它们视为bool类型)

    程序6.8

    #include<iostream>
    #include<cctype>
    int main()
    {
        using namespace std;
        cout << "Enter text for analysis, and type @"
            " to terminate input.
    ";
        char ch;
        int whitespace = 0;
        int digits = 0;
        int chars = 0;
        int punct = 0;
        int others = 0;
    
        cin.get(ch);
        while (ch != '@')
        {
            if (isalpha(ch))
                chars++;
            else if (isspace(ch))
                whitespace++;
            else if (isdigit(ch))
                digits++;
            else if (ispunct(ch))
                punct++;
            else
                others++;
            cin.get(ch);
        }
        cout << chars << " letters, "
            << whitespace << " whitespace, "
            << digits << " digits, "
            << punct << " punctuations, "
            << others << " others.
    ";
        system("pause");
        return 0;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    下面是该程序的运行情况,注意,空白字符计数包括换行符: 
    这里写图片描述

    下表列出了cctype中的字符函数:

    函数名称返回值
    isalnum() 如果参数是字母数字,即字母或数字,该函数返回true
    isalpha() 如果参数是字母,该函数返回true
    iscntrl() 如果参数是控制字符,该函数返回true
    isdigit() 如果参数是数字(0~9),该函数返回true
    isgraph() 如果参数是除空格之外的打印字符,该函数返回true
    islower() 如果参数是小写字母,该函数返回true
    isprint() 如果参数是打印字符(包括空格),该函数返回true
    ispunct() 如果参数是标点符号,该函数返回true
    isspace() 如果参数是标准空白字符,如空格、换行符、回车、水平制表符或者垂直制表符,该函数返回true
    isupper() 如果参数是大写字母,该函数返回true
    isxdigit() 如果参数是十六进制数字,即0~9、a~f或A~F,该函数返回true
    tolower() 如果参数是大写字符,该函数返回其小写,否则返回该参数
    toupper() 如果参数是小写字符,该函数返回其大写,否则返回该参数
  • 相关阅读:
    C# 遍历类字段 及 字段值
    u8 采购到货单中的 业务类型 没有表字典,是系统预置 存入表也是文字: 普通采购 固定资产
    win7 X64 OLEDB读取EXCEL提示“未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序”
    关于 C# select 读取 Excel 数据 为空,或成科学计数法解决方案
    如何设置C#程序在Windows 7 Vista下以管理员权限运行
    C# OLE DB 的连接方式下 对 sql 参数的操作 需要改成 ? 号
    NHibernate (一) 五部曲
    做优秀产品经理
    Python Test in Hudson
    [转载]Openstack Essex安装文档资料
  • 原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/7655136.html
Copyright © 2020-2023  润新知