【C语言】<ctype.h> 字符操作函数(详解+用法+模拟实现)

字符分很多类,对一个字符处理时我们往往要对类型进行判断。本文介绍的函数可以让你免于大于小于号判断条件的书写。

isalpha 字母判断函数

isalpha头文件:ctype.h
isalpha功能:判断字符是否是字母类的(alphabetic)。
包括:a(ASCII值: 97)-z(122) 和 A(65)-Z(90) 。
isalpha函数声明:

int isalpha ( int c );

c是我们输入的,需要被判断的字符。
不是字母类的,返回值0。是字母类的,返回非0值。
isalpha使用实例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	char str[] = "C++";
	for(int i=0;i<3;i++)
	{
		if (isalpha(str[i])) 
			printf("character %c is alphabetic\n", str[i]);
		else 
			printf("character %c is not alphabetic\n", str[i]);
	}
	return 0;
}

代码输出实例
my_isalpha模拟实现:

int my_isalpha(const int c)
{
	if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
	{
		return 1;
	}
	
	return 0;
}

islower 小写字母判断函数

islower头文件:ctype.h
islower功能:判断字符是否是小写字母。
包括:a(ASCII值: 97)-z(122) 。
islower函数声明:

int islower ( int c );

c是我们输入的,需要被判断的字符。
不是小写字母的,返回值0。是小写字母的,返回非0值。
islower使用实例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	char str[] = "Test";
	for (int i = 0; i < 4; i++)
	{
		if (islower(str[i]))
			printf("character %c is lower.\n", str[i]);
	}
	return 0;
}

代码输出实例
my_islower模拟实现:

int my_islower(const int c)
{
	if (c >= 'a' && c <= 'z')
	{
		return 1;
	}

	return 0;
}

isupper 大写字母判断函数

isupper头文件:ctype.h
isupper功能:判断字符是否是大写字母。
包括:A(ASCII码值: 65)-Z(90) 。
isupper函数声明:

int isupper ( int c );

c是我们输入的,需要被判断的字符。
不是大写字母的,返回值0。是大写字母的,返回非0值。
isupper使用实例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	char str[] = "Test";
	for (int i = 0; i < 4; i++)
	{
		if (isupper(str[i]))
			printf("character %c is upper.\n", str[i]);
	}
	return 0;
}

代码输出实例
my_isupper模拟实现:

int my_isupper(const int c)
{
	if (c >= 'A' && c <= 'Z')
	{
		return 1;
	}

	return 0;
}

tolower 大写字母转小写字母函数

tolower头文件:ctype.h
tolower功能:将大写字母转成小写字母,非大写字母不变化。
tolower函数声明:

int tolower ( int c );

c是输入的字符。
如果c是大写字母,返回对应小写字母的ASCII码值。如果不是,则返回c本身的ASCII码值。
tolower使用实例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	int i = 0;
	char str[] = "Test String.";
	while (str[i])
	{
		putchar(tolower(str[i]));
		i++;
	}
	printf("\n%s", str);
	return 0;
}

代码输出实例
my_tolower模拟实现:

int my_tolower(const int c)
{
	if (c >= 'A' && c <= 'Z')
	{
		return c + 32;
	}

	return c;
}

toupper 小写字母转大写字母函数

toupper头文件:ctype.h
toupper功能:将小写字母转成大写字母,非小写字母不变化。
toupper函数声明:

int toupper(int c);

c是输入的字符。
如果c是大写字母,返回对应小写字母的ASCII码值。如果不是,则返回c本身的ASCII码值。
toupper使用实例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	int i = 0;
	char str[] = "Test String.";
	while (str[i])
	{
		putchar(toupper(str[i]));
		i++;
	}
	printf("\n%s", str);
	return 0;
}

代码输出实例
my_toupper模拟实现:

int my_toupper(const int c)
{
	if (c >= 'a' && c <= 'z')
	{
		return c - 32;
	}

	return c;
}

isdigit 十进制数字判断函数

isdigit头文件:ctype.h
isdigit功能:判断字符是否是十进制数字。
包括:0 (ASCII码值: 48)-9(57)
isdigit函数声明:

int isdigit( int c);

c是我们输入的,需要被判断的字符。
不是十进制数字的,返回值0。是十进制数字的,返回非0值。
isdigit使用实例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	char id[] = "qq:2149649773";
	int i = 0;
	while (id[i])
	{
		if (isdigit(id[i]))
			printf("%c", id[i]);
		i++;
	}
	return 0;
}

代码输出实例

my_islower模拟实现:

int my_isdigit(const int c)
{
	if (c >= '0' && c <= '9')
	{
		return 1;
	}

	return 0;
}

isxdigit 十六进制数字判断函数

isxdigit头文件:ctype.h
isxdigit功能:判断字符是否是十六进制数字。
包括:0-9,a-f,A-F。
isxdigit函数声明:

int isxdigit ( int c );

c是我们输入的,需要被判断的字符。
不是十六进制数字的,返回值0。是十六进制数字的,返回非0值。
isxdigit使用实例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	char id[] = "qq:2149649773abcdEF";
	int i = 0;
	while (id[i])
	{
		if (isxdigit(id[i]))
			printf("%c", id[i]);
		i++;
	}
	return 0;
}

代码输出实例

my_isxdigit模拟实现:

int my_isxdigit(const int c)
{
	if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
	{
		return 1;
	}

	return 0;
}

isalnum 字母或数字判断函数

isxdigit头文件:ctype.h
isxdigit功能:判断字符是否是十六进制数字。
包括:0-9,a-f,A-F。
isxdigit函数声明:

int isxdigit ( int c );

c是我们输入的,需要被判断的字符。
不是十六进制数字的,返回值0。是十六进制数字的,返回非0值。
isxdigit使用实例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	char id[] = "qq:2149649773abcdEF";
	int i = 0;
	while (id[i])
	{
		if (isxdigit(id[i]))
			printf("%c", id[i]);
		i++;
	}
	return 0;
}

代码输出实例

my_isxdigit模拟实现:

int my_isxdigit(const int c)
{
	if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
	{
		return 1;
	}

	return 0;
}

因为C/C++不支持像自然语言中的连续比较操作,如0<c<9。所以掌握上面几个函数,可以减少许多麻烦书写。
码字不容易,欢迎关注,点赞,收藏,评论,转发。

  • 25
    点赞
  • 105
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱code的清隆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值