c++ cctype

本文详细介绍了C++标准库中的cctype头文件,它包含了一系列用于处理字符的函数,如判断字符类型、大小写转换等。通过学习,你可以更好地理解和使用这些函数来处理字符串中的字符。

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

#include<iostream>
#include<cctype>
int main(){
    using namespace std;
    cout<<"Enter text for analysis, an 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);
    while(ch!='@'){
        if(isalpha(ch)){//如果参数是字母,返回true
            chars++;
        }else if(isspace(ch)){//如果参数是标准空白字符 返回true
           whitespace++;     
        }else if(isdigit(ch)){//如果参数是数字[0-9],返回true
            digits++;
        }else if(ispunct(ch)){//如果参数是标点符号,返回true
            punct++;
        }else{
            others++;
        }
        cin.get(ch);
    }
    cout<<chars<<" letters, "
        <<whitespace<<" whitespace, "
        <<digits<<" digits, "
        <<punct<<" punctuations, "
        <<others<<" outhers.\n";

    return 0;
}

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( )如果参数是小写字符,则返回其大写,否则返回该参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值