在C语言中如何判断输入的是否为英文字母?
1个回答
展开全部
:#include
<ctype.h>
isalnum()
用来判断一个字符是否为英文字母或数字,相当于
isalpha(c)
||
isdigit(c),其原型为:
int
isalnum(int
c);
【参数】c
为需要检测的字符。
【返回值】若参数c
为字母或数字,若
c
为
0
~
9
a
~
z
A
~
Z
则返回非
0,否则返回
0。
注意,isalnum()为宏定义,非真正函数。
【实例】找出str
字符串中为英文字母或数字的字符。
#include
<ctype.h>main(){char
str[]
=
"123c@#FDsP[e?";int
i;for
(i
=
0;
str[i]
!=
0;
i++)if(isalnum(str[i]))printf("%c
is
an
alphanumeric
character\n",
str[i]);}
#include
<ctype.h>
main(){
char
str[]
=
"123c@#FDsP[e?";
int
i;
for
(i
=
0;
str[i]
!=
0;
i++)
if(isalnum(str[i]))
printf("%c
is
an
alphanumeric
character\n",
str[i]);}
输出结果:
1
is
an
apphabetic
character
2
is
an
apphabetic
character
3
is
an
apphabetic
character
c
is
an
apphabetic
character
F
is
an
apphabetic
character
D
is
an
apphabetic
character
s
is
an
apphabetic
character
P
is
an
apphabetic
character
e
is
an
apphabetic
character
<ctype.h>
isalnum()
用来判断一个字符是否为英文字母或数字,相当于
isalpha(c)
||
isdigit(c),其原型为:
int
isalnum(int
c);
【参数】c
为需要检测的字符。
【返回值】若参数c
为字母或数字,若
c
为
0
~
9
a
~
z
A
~
Z
则返回非
0,否则返回
0。
注意,isalnum()为宏定义,非真正函数。
【实例】找出str
字符串中为英文字母或数字的字符。
#include
<ctype.h>main(){char
str[]
=
"123c@#FDsP[e?";int
i;for
(i
=
0;
str[i]
!=
0;
i++)if(isalnum(str[i]))printf("%c
is
an
alphanumeric
character\n",
str[i]);}
#include
<ctype.h>
main(){
char
str[]
=
"123c@#FDsP[e?";
int
i;
for
(i
=
0;
str[i]
!=
0;
i++)
if(isalnum(str[i]))
printf("%c
is
an
alphanumeric
character\n",
str[i]);}
输出结果:
1
is
an
apphabetic
character
2
is
an
apphabetic
character
3
is
an
apphabetic
character
c
is
an
apphabetic
character
F
is
an
apphabetic
character
D
is
an
apphabetic
character
s
is
an
apphabetic
character
P
is
an
apphabetic
character
e
is
an
apphabetic
character
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询