c语言中的字符函数

1.字符分类函数

c语言中有一系列函数是专门做字符分类的,也就是一个字符属于什么类型的字符。这些函数的使用需要包含一个头文件是ctype.h

可能你看这些感觉很懵,我以islower举例

复制代码
#include<ctype.h>
int main()
{
	int ret=islower('A');
	printf("%d ", ret); //0
	ret = islower('a');
	printf("%d ", ret);//非0
	ret = islower('0');
	printf("%d ", ret);//0
	return 0;
}

islower是判断是否为小写的,但是'A'是大写的,所以返回0。其他也是如此,反正符合判断返回非0,不符合判断返回0.

练习:写一个代码,将字符串中的小写字母改为大写,其他字符不变

提示:小写字母的ASCII-32=大写字母的ASCII

复制代码
int main()
{
	char arr[] = "I am a Student";
	int i = 0;
	while (arr[i] != '\0')
	{
		if (islower(arr[i]))
		{
			arr[i] = arr[i] - 32;
		}
		i++;
	}
	printf("%s", arr);
	return 0;
}

2.字符转换函数

c语言中提供了两个字符转换函数,头文件也是ctype.h

1 int tolower ( int c)//将传进去的参数大写改为小写

2 int toupper ( int c)//将传进去的参数小写改为大写

上面的代码,我们将小写改为大写,是-32的操作,有了转换字符,我们可以直接用toupper函数

复制代码
int main()
{
	char arr[] = "I am a Student";
	int i = 0;
	while (arr[i] != '\0')
	{
		if (islower(arr[i]))
		{
			arr[i] = toupper(arr[i]);
		}
		i++;
	}
	printf("%s", arr);
	return 0;
}
相关推荐
devilnumber11 小时前
Java 递归算法 详解 + 核心要点 + 实战运用 + 避坑指南
java·开发语言·算法
asdfg125896313 小时前
JavaBean是什么?怎么理解?有什么用途?
java·开发语言
dsyyyyy110113 小时前
JavaScript变量
开发语言·javascript·ecmascript
玖玥拾14 小时前
C/C++ 基础笔记(十三)继承
c语言·c++·继承
z落落14 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
allway214 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_4624462314 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash
一个梦醒了14 小时前
安装git bash选项推荐
开发语言·git·bash
ct97815 小时前
React 状态管理方案深度对比
开发语言·前端·react
数量技术宅15 小时前
2026量化前沿:从Reddit热帖到Python实战,如何用赫斯特指数(Hurst)狙击虚假突破?
开发语言·python