如何判断读数是英文字母还是数字 [英] how do I tell whether the read is an English letter or a number

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

问题描述

例如,我从输入中读取了一个字母

我需要判断它是字母还是数字

我正在做的是

char a;

...... //读一个

int true = false;

if(( a> =''0'')&&(a< =''z'')

true = 1;

有没有简单的方法为了它?

非常感谢!

for instance, I read a char from the input
and I need to decide whether it is a letter or a number
What I am doing is
char a;
...... // read a
int true = false;
if( (a >=''0'') && (a <=''z'')
true = 1;
Is there any easy way for it?
Thanks a lot!

推荐答案

" QQ"< ju *** *@yahoo.com>写信息

news:11 ********************** @ y43g2000cwc.googlegr psps.com。 ..
"QQ" <ju****@yahoo.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
例如,我从输入中读取了一个字符
我需要判断它是字母还是数字
我在做什么
char a;
..... //读取
int true = false;
if((a> =''0'')&&(a< =''z'')
true = 1;

有没有简单的方法呢?


未经测试的垃圾如下:


#include< ctype.h >

#include< stdio.h>

#include< string.h>

....

char string [32767];

if(fgets(string,sizeof string,stdin))

{

int i;

int ival;

for(i = 0;我<的strlen(字符串); i ++)

ival = string [i];

if(isalpha(ival))puts(" letter");

if( isdigit(ival))puts(" digit");

if(ispunct(ival))puts(" punctuation");

}

}

非常感谢!
for instance, I read a char from the input
and I need to decide whether it is a letter or a number
What I am doing is
char a;
..... // read a
int true = false;
if( (a >=''0'') && (a <=''z'')
true = 1;
Is there any easy way for it?
untested junk follows:

#include <ctype.h>
#include <stdio.h>
#include <string.h>
....
char string[32767];
if (fgets(string, sizeof string, stdin))
{
int i;
int ival;
for (i = 0; i < strlen(string); i++)
ival = string[i];
if (isalpha(ival)) puts("letter");
if (isdigit(ival)) puts("digit");
if (ispunct(ival)) puts("punctuation");
}
}
Thanks a lot!





QQ写道:

QQ wrote:
例如,我从输入中读取了一个字母
我需要判断它是字母还是数字
我在做什么是
char a ;
..... //读取
int true = false;
if((a> =''0'')&&(a< = 'z'')
true = 1;


这不便携,因为它取决于您的平台代码的方式

字符
和数字。它不能用于ASCII编码。

有没有简单的方法呢?
for instance, I read a char from the input
and I need to decide whether it is a letter or a number
What I am doing is
char a;
..... // read a
int true = false;
if( (a >=''0'') && (a <=''z'')
true = 1;
This is not portable since it depends on the way your platform codes
characters
and digits. It won''t work with ASCII encoding.
Is there any easy way for it?




是的,它被称为isalnum()


Spiros Bousbouras



Yes and it''s called isalnum()

Spiros Bousbouras


Q Q写道:
例如,我从输入中读取了一个字母
我需要判断它是一个字母还是一个数字


看看....宏形式标准库,isalnum(),

可能做你想要的。

我在做什么是
char a;
...... //读取
int true = false;
for instance, I read a char from the input
and I need to decide whether it is a letter or a number
Have a look at the is.... macros form the standard library, isalnum(),
probably does what you want.
What I am doing is
char a;
...... // read a
int true = false;




不是变量名的好选择。


-

Ian Collins。



Not a good choice of variable name.

--
Ian Collins.


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

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