使用 isdigit() 和 isalpha() 命令的最简单方法是什么?

发布于 2024-09-01 03:37:23 字数 333 浏览 5 评论 0原文

我需要简要解释一下 isdigit()isalpha() 这两个命令的工作原理。当然,我在提出问题之前阅读了在线资源,但我尝试过它们但无法让它们工作。使用它们的最简单方法是什么?

我知道它返回一个值,所以我假设我可以像这样使用它:

if(isdigit(someinput)==1)
 return -1;

这是正确的吗?我可以将其用于任何类型的角色吗?我可以将它与浮点数或数组进行比较吗?

假设,我想扫描一个包含数字和字母的文本文件,并确定我正在扫描的内容。这两个命令可以在这种情况下使用吗?

I need a brief explanation on how the two commands isdigit() and isalpha() work. Of course, I read online sources before asking the question, but I tried them and couldn't get them to work. What is the simplest way of using them?

I know it gives back a value, so I'm assuming I can use it like this:

if(isdigit(someinput)==1)
 return -1;

Is that correct? Can I use this for any type of character? And can I compare it with a float number or array?

Suppose, I want to scanf a text file that has numbers and letter and determine what I'm scanning. Can these two commands be used in this context?

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

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

发布评论

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

评论(3

说好的呢 2024-09-08 03:37:23

您通常不想比较返回值,只需将其视为布尔值即可:

if (isalpha(someinput))
    do_whatever();

如果您坚持进行比较,则它需要为!=0,但它是完全多余且毫无意义。

您可以在从输入读取的字符上使用它,这些字符(此时)不是浮点数或任何东西,只是字符组。对于浮点数,其中一些将是数字,但许多也将包含小数点(不是数字),并且可能偶尔包含 e+- 也是如此。

另请注意,在调用任何 is* 函数之前,您通常需要将输入转换为 unsigned char。当某些字符被视为有符号字符时,许多字符集会将它们视为负数,并且将负值传递给其中任何一个都会导致未定义的行为。

You do not normally want to compare the return value, just treat it as a Boolean:

if (isalpha(someinput))
    do_whatever();

If you insist on doing a comparison, it needs to be !=0, but it's entirely redundant and pointless.

You use it on characters that have been read from input, which are not (at that point) floats or anything, just groups of characters. In the case of a float, some of those will be digits, but many will also include decimal points (which aren't digits) and possibly an occasional e, + or - as well.

Also note that you normally need to cast the input to unsigned char before calling any of the is* functions. Many character sets will treat some characters as negative when they're viewed as signed char, and passing a negative value to any of these gives undefined behavior.

抱着落日 2024-09-08 03:37:23

它们不是“命令”,而是函数。函数接受参数并返回值。

#include <ctype.h>
int isdigit( int ch );

这是 isdigit 函数的签名:它表明它将接受 int 值(或者可以转换为 int 的值,例如a char),并将返回一个 int。因此,您不能向其传递一个数组(尽管您可以对 int[] 的每个成员调用它)。

isalpha 的签名是相同的(显然,名称除外)。

该文档说明如下:

说明:函数 isalpha()
如果其参数是 a,则返回非零
字母表中的字母。否则,
返回零。

这意味着您的比较并不适用于所有实现。最好这样做:

if (isdigit(someinput)) {
 return -1;
}

在 C 中,布尔表达式中的 0 将计算为 false,所有非零值将计算为 true。因此,此检查将涵盖返回 -1、5 等的 isdigit 实现。

如果要将这些应用于文本文件中的值,则必须一次读取一个字符,并将收到的字符传递给这些方法。

They are not "commands", they are functions. Functions accept arguments and return values.

#include <ctype.h>
int isdigit( int ch );

This is the signature for the isdigit function: it indicates that it will accept an int value (or something that can be cast to int, like a char), and will return an int. You cannot, therefore, pass it an array (though you can call it on every member of an int[]).

The signature for isalpha is identical (except for the name, obviously).

The documentation says the following:

Description: The function isalpha()
returns non-zero if its argument is a
letter of the alphabet. Otherwise,
zero is returned.

This means your comparison will not be correct for all implementations. Better to do something like:

if (isdigit(someinput)) {
 return -1;
}

In C, 0 will evaluate to false in a boolean expression, and all non-zero values evaluate to true. So this check will cover implementations of isdigit that return -1, 5, whatever.

If you want to apply these to values in a text file, you must read the text one character at a time and pass the characters you receive to those methods.

唠甜嗑 2024-09-08 03:37:23

您可以将它用于任何char类型! (实际上它是 int,我不知道这是否是出于兼容性原因或其他原因,但最常见的用法是字符)

它将确定例如“5”是数字还是字母数字(字母表的一部分)。是的,如果 someInput 的类型为“char”,那么您的用法是正确的

You can use it for any char type! (actually it's int, I don't know whether this is for compatibilty reasons or whatnot, but the most common usage is for characters)

It will determine whether for example '5' is a digit or an alphanumerical (part of the alphabet). And yes, your usage is correct if someInput is of type "char"

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