每天学一点库函数5

库函数

字符函数和字符串函数

本文使用的操作系统是Windows 7 64位处理器。
使用的软件是Microsoft visual studio 2015
在使用字符函数的时候,应该在源文件中添加以下命令
#include <ctype.h>

1、isgraph/isprint

isgraph/isprint的作用是检查输入的字符是否为可打印字符,是的话输出1,不是的话输出0。但是isgraph不包括空格,isprint包括空格。
其语法结构为:
int isgraph(char ch)
int isprint(char ch)
可打印字符的ASCII码范围为 0x21~0x7E 共有94个。

	char ch1, ch2, ch3, ch4, ch5, ch6, ch7;
	ch1 = '\a';
	ch2 = ' ';
	ch3 = '1';
	ch4 = 'a';
	ch5 = 'A';
	ch6 = '{';
	ch7 = '+';
	cout << "isgraph\\a: " << isgraph(ch1) << endl;
	cout << "isgraph : " << isgraph(ch2) << endl;
	cout << "isgraph1: " << isgraph(ch3) << endl;
	cout << "isgrapha: " << isgraph(ch4) << endl;
	cout << "isgraphA: " << isgraph(ch5) << endl;
	cout << "isgraph{: " << isgraph(ch6) << endl;
	cout << "isgraph+: " << isgraph(ch7) << endl;

	cout << "isprint\\a: " << isprint(ch1) << endl;
	cout << "isprint : " << isprint(ch2) << endl;
	cout << "isprint: " << isprint(ch3) << endl;
	cout << "isprint: " << isprint(ch4) << endl;
	cout << "isprint: " << isprint(ch5) << endl;
	cout << "isprint{: " << isprint(ch6) << endl;
	cout << "isprint+: " << isprint(ch7) << endl;

运行以上代码,结果如下。
isgraph/isprint运行示意图
如图所示,由于新版本的影响。对于数字字符来说,输出的结果为4。对于小写字符来说,输出的是2。对于大写字符来说,输出的是1。对于其它符号来说,输出的是16。对于空格来说,isgraph输出的是0,isprint输出的是64。
这是应该注意到的。

2、islower/isupper

如同他们的名字,islower 的作用是判断输入字符是否为小写字母。isupper的作用是判断输入字符是否为大写字符。是的话输出1,不是的话输出0。
其语法结构为:
int islower(char ch)
int isupper(char ch)

	char ch1, ch2, ch3, ch4, ch5;
	ch1 = 'a';
	ch2 = 'A';
	ch3 = '2';
	ch4 = '+';
	ch5 = '\b';
	cout << "islower(a):" << islower(ch1) << endl;
	cout << "islower(A):" << islower(ch2) << endl;
	cout << "islower(2):" << islower(ch3) << endl;
	cout << "islower(+):" << islower(ch4) << endl;
	cout << "islower(\\b):" << islower(ch5) << endl;
	cout << "isupper(a):" << isupper(ch1) << endl;
	cout << "isupper(A):" << isupper(ch2) << endl;
	cout << "isupper(2):" << isupper(ch3) << endl;
	cout << "isupper(+):" << isupper(ch4) << endl;
	cout << "isupper(\\b):" << isupper(ch5) << endl;

程序运行结果如下
islower/isupper运行结果示意图
从图中可以看到,对于小写字母,islower输出的是2。对于大写字母,isupper输出的是1。

3、isspace

本函数的功能是判断输入字符是否为空格、跳格符(制表符)或者换行符。是的话输出1,不是的话输出0。
其语法结构为:
int isspace(char ch)

	char ch1, ch2, ch3, ch4, ch5, ch6;
	ch1 = ' ';
	ch2 = '\t';
	ch3 = '\n';
	ch4 = '\v';
	ch5 = '\f';
	ch6 = '\r';
	cout << "isspace( ):" << isspace(ch1) << endl;
	cout << "isspace(\\t):" << isspace(ch2) << endl;
	cout << "isspace(\\n):" << isspace(ch3) << endl;
	cout << "isspace(\\v):" << isspace(ch4) << endl;
	cout << "isspace(\\f):" << isspace(ch5) << endl;
	cout << "isspace(\\r):" << isspace(ch6) << endl;

运行结果如下所示:
isspace运行结果示意图
如图所示,对于横向制表符(\t)、换行符(\n)、纵向制表符(\v)、换页符(\f)、回车(\r)和空格来说,都会输出8。否则的话会输出0。

4、ispunct

本函数的功能是判断输入字符是否为标点符号,不包括空格。即除字母、数字和空格以外的所有可打印字符。
其语法结构为:
int ispunct(char ch)

	char ch1, ch2, ch3, ch4, ch5, ch6;
	ch1 = '!';
	ch2 = '1';
	ch3 = 'a';
	ch4 = 'A';
	ch5 = '=';
	ch6 = '~';
	cout << "ispunct(!):" << ispunct(ch1) << endl;
	cout << "ispunct(1):" << ispunct(ch2) << endl;
	cout << "ispunct(a):" << ispunct(ch3) << endl;
	cout << "ispunct(A):" << ispunct(ch4) << endl;
	cout << "ispunct(=):" << ispunct(ch5) << endl;
	cout << "ispunct(~):" << ispunct(ch6) << endl;

运行结果如下所示:
ispunct运行结果示意图
如图所示~对于符号来说,输出的是16。

Ω ~未完待续

猜你喜欢

转载自blog.csdn.net/ichliebecamb/article/details/85079766