确定 char 是数字还是字母 [英] Determine if char is a num or letter

查看:29
本文介绍了确定 char 是数字还是字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何判断C中的char,如a9是数字还是字母?

How do I determine if a char in C such as a or 9 is a number or a letter?

是不是更好用:

int a = Asc(theChar);

还是这个?

int a = (int)theChar

推荐答案

您需要在 中使用 isalpha()isdigit() 标准函数><ctype.h>.

You'll want to use the isalpha() and isdigit() standard functions in <ctype.h>.

char c = 'a'; // or whatever

if (isalpha(c)) {
    puts("it's a letter");
} else if (isdigit(c)) {
    puts("it's a digit");
} else {
    puts("something else?");
}

这篇关于确定 char 是数字还是字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆