1、isalnum
功能:判断字符变量c是否为字母或数字
说明:当c为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零。
Example:
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i;
char str[]="c3po...";
i=0;
while (isalnum(str[i]))
{
i++;
}
printf ("The first %d characters are alphanumeric.\n",i);
return 0;
}
output: The first 4 characters are alphanumeric。
2、isalpha
功能:判断字符ch是否为英文字母,当ch为英文字母a-z或A-Z时,返回非零值(不一定是1),否则返回零。
Example:
/* isalpha example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
char str[]="C++";
while (str[i])
{
if (isalpha(str[i]))
{
printf ("character %c is alphabetic\n",str[i]);
}
else
{
printf ("character %c is not alphabetic\n",str[i]);
}
i++;
}
return 0;
}
Output:
character C is alphabetic character + is not alphabetic character + is not alphabetic |
3、isupper
功能:判断字符c是否为大写英文字母,当参数c为大写英文字母(A-Z)时,返回非零值,否则返回零。
/* isupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
char str[]="Test String.\n";
char c;
while (str[i])
{
c=str[i];
if (isupper(c))
{
c=tolower(c); //大写转小写
}
putchar (c);
i++;
}
return 0;
}
4、islower
功能:检查参数c是否为小写英文字母。若参数c为小写英文字母,则返回TRUE,否则返回NULL(0)。
/* islower example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
char str[]="Test String.\n";
char c;
while (str[i])
{
c=str[i];
if (islower(c))
{
c=toupper(c);
}
putchar (c);
i++;
}
return 0;
}
Output:
TEST STRING. |
5、isblank
功能:检查参数c是否为空白字符,也就是判断是否为空白(space)或是制表符(tab)。空白(space)的ASCII码为32,制表符(tab)的ASCAII码则为9。
返回值:如果参数c为空白字符,则返回TRUE,否则返回NULL(0)。
/* isblank example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
char c;
int i=0;
char str[]="Example sentence to test isblank\n";
while (str[i])
{
c=str[i];
if (isblank(c)) //有的库米有
{
c='\n';
}
putchar (c);
i++;
}
return 0;
}
Output:
Example sentence to test isblank
6、isdigit
功能:判断字符c是否为数字
说明:当c为数字0-9时,返回非零值,否则返回零。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
char str[]="1776ad";
int year;
if (isdigit(str[0]))
{
year = atoi (str);
printf ("The year that followed %d was %d.\n",year,year+1);
}
return 0;
}
output:
The year that followed 1776 was 1777
7、ispunct
函数说明:检查参数c是否为标点符号或特殊符号。返回TRUE也就是代表参数c为非空格、非数字和非英文字母。
返回值:若参数c为标点符号或特殊符号,则返回TRUE,否则返回NULL(0)。
/* ispunct example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
int cx=0;
char str[]="Hello, welcome!";
while (str[i])
{
if (ispunct(str[i]))
{
cx++;
}
i++;
}
printf ("Sentence contains %d punctuation characters.\n", cx);
return 0;
}
8、isspaceOutput:Sentence contains 2 punctuation characters.
函数功能:检查参数c是否为空格字符,也就是判断是否为空格(' ')、水平定位字符('\t')、归位键('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。
返回值:若参数c为空格字符,则返回TRUE,否则返回NULL(0)。
#include <stdio.h>
#include <ctype.h>
int main ()
{
char c;
int i=0;
char str[]="Example sentence to test isblank\n";
while (str[i])
{
c=str[i];
if (isblank(c)) c='\n';
putchar (c);
i++;
}
return 0;
}
9、isxdigit
函数说明:检查参数c是否为16进制数字,只要c为下列其中一个情况则返回TRUE。16进制数字:0123456789ABCDEF。
参数类型为int,但是可以直接将char 类型数据传入.
/* isxdigit example */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
char str[]="ffff";
long int number;
if (isxdigit(str[0]))
{
number = strtol (str,NULL,16);
printf ("The hexadecimal number %lx is %ld.\n",number,number);
}
return 0;
}
The hexadecimal number ffff is 65535
10、tolower
功能:把字符转换成小写字母,非字母字符不做出处理
说明:如果c为大写英文字母,则返回对应的小写字母;否则返回原来的值。
11、toupper
功能:将字符c转换为大写英文字母
说明:如果c为小写英文字母,则返回对应的大写字母;否则返回原来的值。
12、综合运用
将十六进制字符串转化成十进制。
#include <stdio.h>
#include <ctype.h>
long atox(char* s)
{
long sum;
while(isspace(*s)) //跳过空格
{
++s;
}
for(sum = 0L;isxdigit(*s);++s)
{
int digit;
if(isdigit(*s))
{
digit = *s - '0';
}
else
{
digit = toupper(*s) - 'A' + 10;
}
sum = sum * 16L + digit;
}
return sum;
}
int main ()
{
char s[] = " F1f3#";
long a = atox(s);
printf("%ld\n",a);
return 0;
}
本文详细介绍了C语言中的ctype.h库函数,包括isalnum、isalpha、isupper、islower、isblank、isdigit、ispunct、isspace、isxdigit、tolower、toupper等,并通过示例展示了它们的功能和用法,最后提供了一个将十六进制字符串转换为十进制的综合应用示例。
626

被折叠的 条评论
为什么被折叠?



