字符函数库cctype

本文介绍了一个使用C++编写的简单程序,该程序能够分析用户输入的文本,并统计出字母、空格、数字、标点符号以及其他字符的数量。此外,还详细解释了几个常用字符检测函数,例如isalpha()、isdigit()等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cctype>
int main()
{
    using namespace std;
    cout<<"Enter text for analysis, and type @"
          "to terminate input.\n";
    char ch;
    int whitespace =0;
    int digits=0;
    int chars=0;
    int punct=0;
    int others=0;

    cin.get(ch);// get first character
    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 "
        <<others<<" others.\n";
    return 0;
}

C++从C继承的一个与字符相关的、非常方便的函数软件包,它可以简化注入确定字符是否为大写字母、数字、标点符号等工作。

函数的原型是在头文件cctype中定义的。

该头文件中的字符函数有:

isalnum()   如果参数是字母数字,即字母或数字,该函数返回true

isalpha()   如果参数是字母,该函数返回true

iscntrl()     如果参数是控制字符,返回true

isdigit()      如果参数哦是数字(0-9),返回true

isgraph()   参数是除空格之外的打印字符,true

islower()  判断是否小写字母

isupper()  判断是否大写字母

isprint()   判断是否打印字符(包括空格)

ispunct() 判断是否标点符号

isspace()  判断是否是标准空白字符,如空格、进纸、换行符、回车、水平制表符或者垂直制表符、

isxdigit()   判断是否为十六进制数字0-9,a-f,A-F

tolower()  参数大写,返回小写,否则返回原参数

toupper() 小写转大写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值