【C++ Primer Plus学习记录】字符函数库cctype

C++从C语言继承了一个与字符相关的、非常方便的函数软件包,它可以简化诸如确定字符是否为大写字母、数字、标点符号等工作,这些函数的原型是在头文件cctype中定义的。

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() 如果参数是小写字符,则返回其大写,否则返回该函数

程序清单6.8演示了一些ctype()库函数。

#if 1
#include
using namespace std;

int main()
{
	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);
	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 << " others.\n";

	system("pause");
	return 0;
}
#endif

你可能感兴趣的:(c++,学习,开发语言,计算机网络,软件工程,visualstudio)