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语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

Go语言中json操作的实现

《Go语言中json操作的实现》本文主要介绍了Go语言中的json操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 一、jsOChina编程N 与 Go 类型对应关系️ 二、基本操作:编码与解码 三、结构体标签(Struc

python语言中的常用容器(集合)示例详解

《python语言中的常用容器(集合)示例详解》Python集合是一种无序且不重复的数据容器,它可以存储任意类型的对象,包括数字、字符串、元组等,下面:本文主要介绍python语言中常用容器(集合... 目录1.核心内置容器1. 列表2. 元组3. 集合4. 冻结集合5. 字典2.collections模块

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

Java中字符编码问题的解决方法详解

《Java中字符编码问题的解决方法详解》在日常Java开发中,字符编码问题是一个非常常见却又特别容易踩坑的地方,这篇文章就带你一步一步看清楚字符编码的来龙去脉,并结合可运行的代码,看看如何在Java项... 目录前言背景:为什么会出现编码问题常见场景分析控制台输出乱码文件读写乱码数据库存取乱码解决方案统一使

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

GO语言短变量声明的实现示例

《GO语言短变量声明的实现示例》在Go语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路