C语言ctype.h头文件字符测试函数

2024-01-13 10:20

本文主要是介绍C语言ctype.h头文件字符测试函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C/C++语言ctype.h头文件字符测试函数

C有一系列专门处理字符的函数,在ctype.h中包含了这些函数的原型,当然在C++中,这些函数被放在了cctype中。

作用

这些函数接收一个字符作为参数,如果该字符属于某个特殊的类别,就返回一个真值(非零值),否则,返回假值。

要注意的是,被传进的参数应该为整数类型(ASCLL码形式),如果你传递进去了一个浮点数,那么它将会被转换为整数。

字符类型

可以传入的字符包括两种类型:

  • 可打印字符(Printable Characters):这类字符能够显示在终端上。
  • 控制字符(Control Characters):这类字符通常是为了执行特定的操作而存在的

库函数

Chinese:

序号函数描述返回值
1.isalnum()这个函数用来判断字符是否是字母或数字如果传入的参数不是字母或数字,返回0。如果传入的参数是字母或数字,返回非零值。
2.isalpha()这个函数用来判断字符是否是字母如果传入的参数不是字母,返回0。如果传入的参数是字母,返回非零值。
3.isblank()这个函数用来判断字符是否是空格如果传入的参数不是空格,返回0。如果传入的参数是空格,返回非零值。
4.iscntrl()这个函数用来判断字符是否是控制字符(\n, \b, \t, \r)。如果传入的参数不是控制字符,返回0。如果传入的参数是控制字符,返回非零值。
5.isdigit()这个函数用来判断字符是否是数字。如果传入的参数不是数字,返回0。如果传入的参数是数字,返回非零值。
6.islower()这个函数用来判断字符是否是小写字母。如果传入的参数不是小写字母,返回0。如果传入的参数是小写字母,返回非零值。
7.isprint()这个函数用来判断字符是否是可打印字符。如果传入的参数不是可打印字符,返回0。如果传入的参数是可打印字符,返回非零值。
8.ispunct()这个函数用来判断字符是否是标点符号(既不是字母或数字也不是空格的字符)。如果传入的参数不是标点符号,返回0。如果传入的参数是标点符号,返回非零值。
9.isspace()这个函数用来判断字符是否是空白字符。如果传入的参数是空白字符,返回非零值。如果传入的参数不是空白字符,返回0。
10.isupper()这个函数用来判断字符是否是大写字母。如果传入的参数是大写字母,返回非零值。如果传入的参数不是大写字母,返回0。
11.isxdigit()这个函数用来判断字符是否是十六进制数字。如果传入的参数是十六进制数字,返回非零值。如果传入的参数不是十六进制数字,返回0。
12.tolower()这个函数用来把大写字母转换成小写字母。返回对应的小写字母。
13.toupper()这个函数用来把小写字母转换成大写字母。返回对应的大写字母。

English:

S.NoFunctionDescriptionReturn Values
1.isalnum()This function identifies the alphanumeric charactersReturns 0 if the passed argument is non – alphanumeric character. Returns non zero value if the passed argument is alphanumeric character.
2.isalpha()This function identifies the alphabets from other charactersReturns 0 if the passed argument is not an alphabet. Returns non zero value if the passed argument is an alphabet.
3.isblank()This function identifies the blank spaces from other charactersReturns 0 if the passed argument is not a blank space. Returns nonzero value if the passed argument is a blank space.
4.iscntrl()This function identifies the control characters(\n, \b, \t, \r).Returns 0 if the passed argument is not a control character. Returns nonzero value if the passed argument is a control character.
5.isdigit()This function identifies numbers in character.Returns 0 if the passed argument is not a number. Returns nonzero value if the passed argument is a number.
6.islower()This function identifies the lowercase alphabets.Returns 0 if the passed argument is not a lowercase alphabet. Returns nonzero value if the passed argument is a lowercase alphabet.
7.isprint()This function identifies the printable characters.Returns 0 if the passed argument is a non printable character. Returns nonzero value if the passed argument is a printable character.
8.ispunct()This function identifies punctuation characters (characters that are neither alphanumeric nor space).Returns 0 if the passed argument is not a punctuation character. Returns nonzero value if the passed argument is a punctuation character.
9.isspace()This function identifies white-space characters.Returns 0 if the passed argument is not a white-space character. Returns nonzero value if the passed argument is a white-space character.
10.isupper()This function identifies the uppercase alphabets.Returns 0 if the passed argument is not an uppercase alphabet.Returns nonzero value if the passed argument is an uppercase alphabet.
11.isxdigit()This function checks whether the argument is a hexadecimal digit.Returns 0 if the argument is not a hexadecimal digit, returns a nonzero value if it is.
12.tolower()This function converts an uppercase letter to a lowercase letter.Returns the lowercase letter corresponding to the uppercase letter.
13.toupper()This function converts a lowercase letter to an uppercase letter.Returns the uppercase letter corresponding to the lowercase letter.

下面的内容是一些例子:

示例1、以下程序识别字母、数字的数量

#include <stdio.h> 
//头文件
#include <ctype.h> void identify_alpha_numeric(char a[]) 
{ int count_alpha = 0, count_digit = 0; for (int i = 0; a[i] != '\0'; i++) { // 检查这个字符是不是字母if (isalpha(a[i])) count_alpha++; // 检查这个字符是不是数字 if (isdigit(a[i])) count_digit++; } printf("The number of alphabets are %d\n", count_alpha); printf("The number of digits are %d", count_digit); 
} int main() 
{ // 字符串初始化char a[] = "Hi 1234, "" Welcome to GeeksForGeeks"; identify_alpha_numeric(a); 
} 

OUTPUT:

The number of alphabets are 24
The number of digits are 4

示例2、以下程序识别大写和小写字母的数量并将大写字母转换为小写字母:

#include <stdio.h> // 头文件
#include <ctype.h> char* identify_convert_ul(char a[]) 
{ int count_upper = 0, count_lower = 0; for (int i = 0; a[i] != '\0'; i++) { // 检查大写字母 if (isupper(a[i])) { count_upper++; a[i] = tolower(a[i]); } //  检查小写字母else if (islower(a[i])) { count_lower++; a[i] = toupper(a[i]); } } printf("No. of uppercase characters are %d\n", count_upper); printf("No. of lowercase characters are %d", count_lower); return a; 
} int main() 
{ // 字符串初始化 char a[] = "Hi, Welcome to GeeksForGeeks"; char* p; p = identify_convert_ul(a); printf("%s", p); 
} 

OUTPUT:

No. of uppercase alphabets are 5
No. of lowercase alphabets are 19
hI, wELCOME TO gEEKSfORgEEKS

示例 3:以下程序在新行中打印每个单词:

#include <stdio.h> // 头文件
#include <ctype.h> char* print_word(char a[]) 
{ for (int i = 0; a[i] != '\0'; i++) { // 替换空格为换行符'\n'if (isblank(a[i])) a[i] = '\n'; } return a; 
} 
int main() 
{ // 字符串初始化char a[] = "Hello Everyone."" Welcome to GeeksForGeeks portal. "; char* p; p = print_word(a); printf("%s", p); 
} 

OUTPUT:

Hello
Everyone.
Welcome
to
GeeksForGeeks
portal.

参考文献:
https://www.geeksforgeeks.org/ctype-hcctype-library-in-c-c-with-examples/

这篇关于C语言ctype.h头文件字符测试函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/601127

相关文章

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

Go语言中泄漏缓冲区的问题解决

《Go语言中泄漏缓冲区的问题解决》缓冲区是一种常见的数据结构,常被用于在不同的并发单元之间传递数据,然而,若缓冲区使用不当,就可能引发泄漏缓冲区问题,本文就来介绍一下问题的解决,感兴趣的可以了解一下... 目录引言泄漏缓冲区的基本概念代码示例:泄漏缓冲区的产生项目场景:Web 服务器中的请求缓冲场景描述代码

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

Go语言中Recover机制的使用

《Go语言中Recover机制的使用》Go语言的recover机制通过defer函数捕获panic,实现异常恢复与程序稳定性,具有一定的参考价值,感兴趣的可以了解一下... 目录引言Recover 的基本概念基本代码示例简单的 Recover 示例嵌套函数中的 Recover项目场景中的应用Web 服务器中

C#如何去掉文件夹或文件名非法字符

《C#如何去掉文件夹或文件名非法字符》:本文主要介绍C#如何去掉文件夹或文件名非法字符的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#去掉文件夹或文件名非法字符net类库提供了非法字符的数组这里还有个小窍门总结C#去掉文件夹或文件名非法字符实现有输入字

Go语言中使用JWT进行身份验证的几种方式

《Go语言中使用JWT进行身份验证的几种方式》本文主要介绍了Go语言中使用JWT进行身份验证的几种方式,包括dgrijalva/jwt-go、golang-jwt/jwt、lestrrat-go/jw... 目录简介1. github.com/dgrijalva/jwt-go安装:使用示例:解释:2. gi

Go 语言中的 Struct Tag 的用法详解

《Go语言中的StructTag的用法详解》在Go语言中,结构体字段标签(StructTag)是一种用于给字段添加元信息(metadata)的机制,常用于序列化(如JSON、XML)、ORM映... 目录一、结构体标签的基本语法二、json:"token"的具体含义三、常见的标签格式变体四、使用示例五、使用