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

相关文章

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

Go语言并发之通知退出机制的实现

《Go语言并发之通知退出机制的实现》本文主要介绍了Go语言并发之通知退出机制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、通知退出机制1.1 进程/main函数退出1.2 通过channel退出1.3 通过cont

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

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 服务器中的请求缓冲场景描述代码