c标准库解释(二)

 ctype.h

1. int isgraph (int c)

        ”isgraph“用于检查一个字符是否是可打印的图形字符(即不是空格、控制字符或删除字符)。如果字符是可打印的图形字符,函数返回非零值,否则返回零。

例子:

#include <stdio.h>
#include <ctype.h>

int main() {
	
	char c1 = '1';
	char c2 = 'm';
	char c3 = ' ';
	char c4 = '\t';

	printf("isgraph(c1) = %d\n", isgraph(c1));//isgraph(c1) = 4
	printf("isgraph(c2) = %d\n", isgraph(c2));//isgraph(c2) = 2
	printf("isgraph(c3) = %d\n", isgraph(c3));//isgraph(c3) = 0
	printf("isgraph(c4) = %d\n", isgraph(c4));//isgraph(c4) = 0

	return 0;
}

        上述例子中“1”和“m”是数字和字母为可打印字符,所以返回值为非0字符,后两个是空白和控制字符,为不可打印的字符,所以返回值为0.
        在实际应用中,例如在处理纯文本时,我们可能想要排除空格,此时isgraph()函数就会非常有用。例如,如果我们要检查一个字符串中哪些字符是可打印的,我们可以使用以下代码:

#include <stdio.h>
#include <ctype.h>

int main() {
	char str[] = "a wgraph";
	for (int i = 0; str[i]; i++) {
		if (isgraph(str[i])) {
			printf("'%c' is a graphic character.\n", str[i]);
		}
		else {
			printf("'%c' is not a graphic character.\n", str[i]);
		}
	}
	return 0;
}

结果如下:

'a' is a graphic character.
' ' is not a graphic character.
'w' is a graphic character.
'g' is a graphic character.
'r' is a graphic character.
'a' is a graphic character.
'p' is a graphic character.
'h' is a graphic character.

2. int islower (int c)

        islower()函数用于检查一个字符是否为小写字母。它接受一个整数作为参数,这个整数通常是字符的ASCII值。如果传入的字符参数是小写字母,则返回非0值;如果不是小写字母,则返回0。

例如:

#include <stdio.h>
#include <ctype.h>

int main() {

	char c1 = 'a';
	char c2 = 'B';
	char c3 = '1';
	char c4 = '\t';

	printf("islower(c1) = %d\n", islower(c1));//islower(c1) = 2
	printf("islower(c2) = %d\n", islower(c2));//islower(c2) = 0
	printf("islower(c3) = %d\n", islower(c3));//islower(c3) = 0
	printf("islower(c4) = %d\n", islower(c4));//islower(c4) = 0

	return 0;
}

         a是小写字母,所以返回非0值,后三个不是小写字母,所以返回0。

        “islower()”函数主要用于在进行文本处理时区分大小写。例如,用户输入的数据可能需要转换为小写字母,以便进行统一的处理。另外,“islower()”函数还可用于验证输入是否符合特定的格式要求,比如要求所有输入的字符都必须是小写字母。

3. int isprint (int c)

        isprint()函数用于检查一个字符是否为可打印字符,包括空格字符。如果传入的字符参数是可打印的(包括空格字符),则返回非0值;如果不是可打印字符,则返回0。

例如:

#include <stdio.h>
#include <ctype.h>

int main() {

	char c1 = 'a';
	char c2 = ' ';
	char c3 = '1';
	char c4 = '\t';

	printf("isprint(c1) = %d\n", isprint(c1));//isprint(c1) = 2
	printf("isprint(c2) = %d\n", isprint(c2));//isprint(c2) = 64
	printf("isprint(c3) = %d\n", isprint(c3));//isprint(c3) = 4
	printf("isprint(c4) = %d\n", isprint(c4));//isprint(c4) = 0

	return 0;
}

        前三个为可打印字符或空格,所以返回非0字符,最后一个是控制字符,为非打印字符,所以返回0。

        isprint()函数的主要用途是在进行文本处理时区分可打印字符和控制字符。可打印字符通常指的是人类可以直接阅读的字符,如字母、数字和标点符号等。而控制字符则用于控制设备操作,如换行符('\n')、制表符('\t')等,这些字符通常不直接显示为可见字符。通过使用isprint()函数,我们可以判断一个字符是否可以被打印出来并显示在屏幕上。
        在实际应用中,例如在处理用户输入的数据时,我们可能希望忽略控制字符,只处理可打印字符。这时可以使用isprint()函数来实现这一功能。另外,在编写程序时,如果需要将字符输出到屏幕上,可以使用isprint()函数来确保输出的字符是可打印的,从而避免显示乱码或控制字符。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值