判断 char 是数字还是字母

发布于 2024-12-22 14:26:15 字数 202 浏览 4 评论 0原文

如何确定 C 中的 char(例如 a9)是数字还是字母?

使用: 还是这个更好

int a = Asc(theChar);

int a = (int)theChar

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

Is it better to use:

int a = Asc(theChar);

or this?

int a = (int)theChar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你是暖光i 2024-12-29 14:26:15

您需要在 中使用 isalpha()isdigit() 标准函数。

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?");
}

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?");
}
丢了幸福的猪 2024-12-29 14:26:15

字符只是整数,因此您实际上可以将字符与文字进行直接比较:

if( c >= '0' && c <= '9' ){

这适用于所有字符。 查看您的 ascii 表

ctype.h 还提供了为您执行此操作的函数。

chars are just integers, so you can actually do a straight comparison of your character against literals:

if( c >= '0' && c <= '9' ){

This applies to all characters. See your ascii table.

ctype.h also provides functions to do this for you.

Saygoodbye 2024-12-29 14:26:15

包含一系列用于确定 char 表示字母还是数字的函数,例如 isalpha>isdigitisalnum

int a = (int)theChar 无法执行您想要的操作的原因是 a 仅保存表示特定字符​​的整数值。例如,'9' 的 ASCII 数字是 57,'a' 的 ASCII 数字是 97。

同样对于 ASCII:

  • 数字 - if (theChar >= ' 0' && theChar <= '9')
  • 按字母顺序 -
    if (theChar >= 'A' && theChar <= 'Z' || theChar >= 'a' && theChar <= 'z')

看一下 ASCII 表供您自己查看。

<ctype.h> includes a range of functions for determining if a char represents a letter or a number, such as isalpha, isdigit and isalnum.

The reason why int a = (int)theChar won't do what you want is because a will simply hold the integer value that represents a specific character. For example the ASCII number for '9' is 57, and for 'a' it's 97.

Also for ASCII:

  • Numeric - if (theChar >= '0' && theChar <= '9')
  • Alphabetic -
    if (theChar >= 'A' && theChar <= 'Z' || theChar >= 'a' && theChar <= 'z')

Take a look at an ASCII table to see for yourself.

↙温凉少女 2024-12-29 14:26:15

这些都没有任何用处。使用标准库中的 isalpha()isdigit()。它们位于 中。

Neither of these does anything useful. Use isalpha() or isdigit() from the standard library. They're in <ctype.h>.

俯瞰星空 2024-12-29 14:26:15

通常,您可以使用简单的条件检查 ASCII 字母或数字

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
    /*This is an alphabet*/
}

对于数字,您可以使用

if (ch >= '0' && ch <= '9')
{
    /*It is a digit*/
}

But,因为 C 中的字符在内部被视为 ASCII 值 您也可以使用 ASCII 值来检查相同的内容。

如何检查字符是否为数字或字母

You can normally check for ASCII letters or numbers using simple conditions

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
    /*This is an alphabet*/
}

For digits you can use

if (ch >= '0' && ch <= '9')
{
    /*It is a digit*/
}

But since characters in C are internally treated as ASCII values you can also use ASCII values to check the same.

How to check if a character is number or letter

阳光的暖冬 2024-12-29 14:26:15

如果 (theChar >= '0' && theChar <='9') 它是一个数字。你明白了。

If (theChar >= '0' && theChar <='9') it's a digit. You get the idea.

茶底世界 2024-12-29 14:26:15

c >= '0' && 上的 C99 标准c <= '9'

c >= '0' && c <= '9'在另一个答案中提到)有效,因为 C99 N1256 标准草案 5.2.1“字符集”说:

在源和执行基本字符集中,
上述十进制数字列表中0之后的每个字符的值应比前一个字符的值大1。

但不保证 ASCII。

C99 standard on c >= '0' && c <= '9'

c >= '0' && c <= '9' (mentioned in another answer) works because C99 N1256 standard draft 5.2.1 "Character sets" says:

In both the source and execution basic character sets, the
value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

ASCII is not guaranteed however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文