测试字符类型的C程序
"ctype.h"库中提供了一些预定义函数,用于分析字符输入并进行转换。
分析函数
以下是字符分析函数列表:
| 函数 | 检查输入字符是否为 |
|---|---|
| isalpha | 字母 |
| isdigit | 数字 |
| isspace | 空格、换行符或制表符 |
| ispunct | 特殊符号 |
| islower | 小写字母 |
| isupper | 大写字母 |
| isalnum | 字母或数字 |
转换函数
以下是转换函数列表:
| 函数 | 转换 |
|---|---|
| tolower() | 将大写字母转换为小写字母 |
| toupper() | 将小写字母转换为大写字母 |
程序
以下是用于测试字符类型的C程序,它使用了字符分析和转换函数:
#include <stdio.h>
#include <ctype.h>
main(){
char character;
printf("Press any key digit or alphabet
");
character = getchar();
if (isalpha(character) > 0)
printf("The character is a letter.");
else
if (isdigit (character) > 0)
printf("The character is a digit.");
else
printf("The character is not alphanumeric.");
}输出
执行上述程序后,将产生以下结果:
Run 1: Press any key digit or alphabet 3 The character is a digit. Run 2: Press any key digit or alphabet G The character is a letter. Run 3: Press any key digit or alphabet & The character is not alphanumeric.
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP