蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 <ctype.h> 字符分类、边界字符类及大小写转换(含 (unsigned char) 安全调用)

C本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。explore-standard-library-ctype-h.c
explore-standard-library-ctype-h.c50 行
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

int main(void) {
    const char sample[] = "a1 B? \n";
    int n_alpha = 0, n_digit = 0, n_space = 0, n_print = 0, n_punct = 0;
    for (int i = 0; sample[i] != '\0'; i++) {
        unsigned char uc = (unsigned char)sample[i];
        if (isalpha(uc)) n_alpha++;
        if (isdigit(uc)) n_digit++;
        if (isspace(uc)) n_space++;
        if (isprint(uc)) n_print++;
        if (ispunct(uc)) n_punct++;
    }
    bool c1_pass = (n_alpha == 2 && n_digit == 1 && n_space == 3 && n_print == 6 && n_punct == 1);

    int alnum_ok = isalnum((unsigned char)'7') != 0;
    int cntrl_ok = iscntrl((unsigned char)'\x7f') != 0 && iscntrl((unsigned char)'\n') != 0;
    int graph_ok = isgraph((unsigned char)'!') != 0 && isgraph((unsigned char)' ') == 0;
    int lower_ok = islower((unsigned char)'z') != 0 && islower((unsigned char)'Z') == 0;
    int upper_ok = isupper((unsigned char)'Z') != 0 && isupper((unsigned char)'z') == 0;
    int xdigit_ok = isxdigit((unsigned char)'F') != 0 && isxdigit((unsigned char)'g') == 0;
    int blank_ok = isblank((unsigned char)'\t') != 0 && isblank((unsigned char)'\n') == 0;
    int eof_ok = isalpha(EOF) == 0;
    int ext_sum = alnum_ok + cntrl_ok + graph_ok + lower_ok + upper_ok + xdigit_ok + blank_ok + eof_ok;
    bool c2_pass = (ext_sum == 8);

    char low = (char)tolower((unsigned char)'A');
    char up = (char)toupper((unsigned char)'a');
    char keep = (char)toupper((unsigned char)'9');
    int eof_conv = tolower(EOF);
    bool c3_pass = (low == 'a' && up == 'A' && keep == '9' && eof_conv == EOF);

    printf(
        "{"
        "\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
        "\"cases\":["
        "{\"name\":\"classify_sample_chars\",\"expected\":\"alpha=2,digit=1,space=3,print=6,punct=1\",\"actual\":\"alpha=%d,digit=%d,space=%d,print=%d,punct=%d\",\"passed\":%s},"
        "{\"name\":\"extended_char_classes\",\"expected\":8,\"actual\":%d,\"passed\":%s},"
        "{\"name\":\"case_conversion_behavior\",\"expected\":\"a,A,9,eof=1\",\"actual\":\"%c,%c,%c,eof=%d\",\"passed\":%s}"
        "]"
        "}\n",
        n_alpha, n_digit, n_space, n_print, n_punct, c1_pass ? "true" : "false",
        ext_sum, c2_pass ? "true" : "false",
        low, up, keep, (eof_conv == EOF) ? 1 : 0, c3_pass ? "true" : "false"
    );
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。

入口文件:projects/c-standard-library-ctype-h-lab/explore-standard-library-ctype-h.c

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-standard-library-ctype-h.c,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C

课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip

下载课程关联工程包:C

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3242.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习