关于c++11中string中cctype头文件中的函数

本文介绍了C++标准库中的cctype头文件,包括isalnum、isalpha、iscntrl等函数的使用,展示了如何检测字符串中的字母、数字、控制字符等,并通过示例代码演示了如何统计字符串中标点符号的数量。
摘要由CSDN通过智能技术生成

1.cctype头文件的函数

1.isalnum(c) 当c是字母或者数字时为真

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
	string s;
	cin >> s;   //输入字符串
	int sum = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (isalnum(s[i])) //如果是字母或者数字据徐
		{
			continue;
		}
		else
		{
			cout << "it's not number or alphabet" << endl;
		}
	}
	cout << s << endl;
}

2.isalpha(c)当C是字母为真

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
	string s;
	cin >> s;   //输入字符串
	int sum = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (isalpha(s[i])) //如果是字母据徐
		{
			continue;
		}
		else
		{
			cout << "it's not number or alphabet" << endl;
		}
	}
	cout << s << endl;
}

3.iscntrl(c)当c是控制字母时为真

4.isdigit(c)当c时数字为真

5.isgraph(c)当C不是空格但可打印时为真

6.islower(c)当C时小写字母为真

7.isprint(c)当C是可打印字符为真

8.ispunct(c)当C是标点符号为真

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
	string s;
	cin >> s;   //输入字符串
	int sum = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (ispunct(s[i])) //统计有多少标点符号
		{
			sum++;
		}
	}
	cout << sum << endl;
	cout << s << endl;
}

9.isspace(c)  当C是空白时为真

10.isupper(c)当C是大写字母为真

11.isxdigit(c)当C是十六进制数字为真

12.tolower(c)如果是大写字母,输出对应的小写字母;否则原样输出C

13.toupper(c)如果是小写字母,输出对应的大写字母;否则原样输出C

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值