isalnum
isalnum函数可以判断一个字符是否是字母或者数字。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[] = "123c@#$GDG[]?]";
int i;
for(i = 0; str[i] != 0; i++)
{
if(isalnum(str[i]))
{
printf("%c is an alphanumeric character\n", str[i]);
}
}
return 0;
}
isalpha
isalpha函数判断一个字符是否为字母
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[] = "123c@#$GDG[]?]";
int i;
for(i = 0; str[i] != 0; i++)
{
if(isalpha(str[i]))
{
printf("%c is an alpha character\n", str[i]);
}
}
return 0;
}
isascii
isascii函数可以判断字符是否为ascii字符,也就是其值是否在0-127之间。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int i;
for(i = 125; i < 130; i++)
{
if(isascii(i))
{
printf("%d is ascii character:%c\n", i, i);
}
else
{
printf("%d is not an ascii character.\n", i);
}
}
return 0;
}
iscntrl
iscntrl函数可以判断字符是不是控制字符,也就是其值是否在0-30之间
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int i;
for(i = 25; i < 35; i++)
{
if(iscntrl(i))
{
printf("%d is control character:%c\n", i, i);
}
else
{
printf("%d is not an control character.\n", i);
}
}
return 0;
}
isdigit
isdigit判断字符是否为数字。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[] = "123c@#$GDG[]?]";
int i;
for(i = 0; str[i] != 0; i++)
{
if(isdigit(str[i]))
{
printf("%c is an digit character\n", str[i]);
}
}
return 0;
}
isgraph
isgraph判断字符是否可打印,此宏判断空格不可以被打印出。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[] = "123c ;@#$GDG[]?]";
int i;
for(i = 0; str[i] != 0; i++)
{
if(isgraph(str[i]))
{
printf("%c is an low-case character\n", str[i]);
}
}
return 0;
}
islower
islower判断字符是否为小写字母。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[] = "123c ;@#$GDG[]?]";
int i;
for(i = 0; str[i] != 0; i++)
{
if(islower(str[i]))
{
printf("%c is an lower-case character\n", str[i]);
}
}
return 0;
}
isprint
isprint判断字符是否可打印,控制字符就是不可以被打印出来的。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[] = "a5 @;";
int i;
for(i = 0; str[i] != 0; i++)
{
if(isprint(str[i]))
{
printf("%c is an printable character\n", str[i]);
}
}
char c = 23;
if(isprint(c))
{
printf("c is printable\n");
}
return 0;
}
isspace
isspace判断字符是不是空格。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[] = "123c ;@#$G DG[]?]";
int i;
for(i = 0; str[i] != 0; i++)
{
if(isspace(str[i]))
{
printf("str[%d] is a white-space character: %d\n", i, str[i]);
}
}
return 0;
}
ispunct
ispunct判断字符是否是标点符号或者特殊字符。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[] = "123c ;@#$G DG[]?]";
int i;
for(i = 0; str[i] != 0; i++)
{
if(ispunct(str[i]))
{
printf("%c\n", str[i]);
}
}
return 0;
}