<ctype.h> 是一个标准的 C 语言库,提供了一系列用于字符处理的函数。这些函数可以用来判断字符类型、转换字符大小写等。下面将详细介绍 <ctype.h> 中的各个函数及其使用方法。
字符分类函数
isalnum()
该函数用于检查给定的字符是否是字母或数字。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
if (isalnum(ch)) {
printf("'%c' 是字母或数字\n", ch);
} else {
printf("'%c' 不是字母或数字\n", ch);
}
return 0;
}isalpha()
该函数用于检查给定的字符是否是字母。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'b';
if (isalpha(ch)) {
printf("'%c' 是字母\n", ch);
} else {
printf("'%c' 不是字母\n", ch);
}
return 0;
}isdigit()
该函数用于检查给定的字符是否是数字。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = '7';
if (isdigit(ch)) {
printf("'%c' 是数字\n", ch);
} else {
printf("'%c' 不是数字\n", ch);
}
return 0;
}islower()
该函数用于检查给定的字符是否是小写字母。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'x';
if (islower(ch)) {
printf("'%c' 是小写字母\n", ch);
} else {
printf("'%c' 不是小写字母\n", ch);
}
return 0;
}isupper()
该函数用于检查给定的字符是否是大写字母。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'Y';
if (isupper(ch)) {
printf("'%c' 是大写字母\n", ch);
} else {
printf("'%c' 不是大写字母\n", ch);
}
return 0;
}isprint()
该函数用于检查给定的字符是否是可打印字符。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = '!';
if (isprint(ch)) {
printf("'%c' 是可打印字符\n", ch);
} else {
printf("'%c' 不是可打印字符\n", ch);
}
return 0;
}isspace()
该函数用于检查给定的字符是否是空白字符,包括空格、制表符、换行符等。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = '\t';
if (isspace(ch)) {
printf("'%c' 是空白字符\n", ch);
} else {
printf("'%c' 不是空白字符\n", ch);
}
return 0;
}isxdigit()
该函数用于检查给定的字符是否是十六进制数字。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'F';
if (isxdigit(ch)) {
printf("'%c' 是十六进制数字\n", ch);
} else {
printf("'%c' 不是十六进制数字\n", ch);
}
return 0;
}字符转换函数
tolower()
该函数用于将大写字母转换为小写字母。
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'T';
printf("转换前: '%c', 转换后: '%c'\n", ch, tolower(ch));
return 0;
}toupper()
该函数用于将小写字母转换为大写字母。
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'g';
printf("转换前: '%c', 转换后: '%c'\n", ch, toupper(ch));
return 0;
}其他函数
isascii()
该函数用于检查给定的字符是否是一个 ASCII 字符。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 128;
if (isascii(ch)) {
printf("'%d' 是 ASCII 字符\n", ch);
} else {
printf("'%d' 不是 ASCII 字符\n", ch);
}
return 0;
}iscntrl()
该函数用于检查给定的字符是否是控制字符。
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = '\a'; // 响铃控制字符
if (iscntrl(ch)) {
printf("'%c' 是控制字符\n", ch);
} else {
printf("'%c' 不是控制字符\n", ch);
}
return 0;
}示例代码
下面是一段综合运用 <ctype.h> 中函数的示例代码:
// www.javascriptcn.com code example
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello World! 123";
for (int i = 0; str[i]; i++) {
if (isalpha(str[i])) {
printf("%c 是字母\n", str[i]);
} else if (isdigit(str[i])) {
printf("%c 是数字\n", str[i]);
} else if (isspace(str[i])) {
printf("%c 是空白字符\n", str[i]);
} else {
printf("%c 是其他字符\n", str[i]);
}
}
return 0;
}以上是 <ctype.h> 库中常用函数的介绍及示例代码。通过这些函数,可以方便地进行字符分类和转换操作,从而简化程序开发过程。