【c/c++】isdigit()函数

本文详细介绍了C语言中isdigit函数的使用方法及功能。isdigit函数用于检查参数是否为十进制数字字符,若参数c为阿拉伯数字0~9,则返回非0值;否则返回0。文章通过一个代码示例展示了如何利用isdigit函数来判断字符串中的每个字符是否为数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

isdigit函数

isdigit是计算机C(C++)语言中的一个函数,主要用于检查其参数是否为十进制数字字符。

函数定义:int isdigit(int c)
函数说明:检查参数是否为十进制数字字符(判断括号内是否为0~9的数字。)
函数所需头文件:#include<cctype>
返回值:若参数c为阿拉伯数字0~9,则返回非0值,否则返回0。

代码示例

	    //isdigit:判断字符是否为阿拉伯数字0~9
		#include<iostream>
		#include<cctype>
		 
		using namespace std;
		 
		int main()
		{
		    string str = "123456789asdfghjkl~!@#$%";
		    for(int i = 0; str[i] != 0; ++i)
		    {
		        if(isdigit(str[i]))
		            cout << str[i] << " is  digit" << endl;
		        else
		            cout << str[i] << " is not digit!" << endl;
		    }
		    
		    return 0;
		}

打印输出

标准输出:0 is  digit
1 is  digit
2 is  digit
3 is  digit
4 is  digit
5 is  digit
6 is  digit
7 is  digit
8 is  digit
9 is  digit
a is not digit!
s is not digit!
d is not digit!
f is not digit!
g is not digit!
h is not digit!
j is not digit!
k is not digit!
l is not digit!
~ is not digit!
! is not digit!
@ is not digit!
# is not digit!
$ is not digit!
% is not digit!


引经据典

https://baike.baidu.com/item/isdigit/9455880?fr=aladdin

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谁吃薄荷糖

你获得知识,我获得财富,双赢!

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

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

打赏作者

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

抵扣说明:

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

余额充值