#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main(void) {
char buf[64];
strcpy(buf, "Hello");
strcat(buf, ", World!");
size_t len = strlen(buf);
int cmp_lt = (strcmp("Hello", "World") < 0) ? 1 : 0;
int ncmp_eq = (strncmp(buf, "Hello", 5) == 0) ? 1 : 0;
bool c1_pass = (strcmp(buf, "Hello, World!") == 0 && len == 13 && cmp_lt == 1 && ncmp_eq == 1);
const char *s = "Hello, World!";
const char *first_o = strchr(s, 'o');
const char *last_o = strrchr(s, 'o');
const char *sub = strstr(s, "World");
const char *pbrk = strpbrk("abc123def", "0123456789");
size_t spn = strspn("abc123", "abc");
size_t cspn = strcspn("abc123", "0123456789");
long idx_first = first_o ? (long)(first_o - s) : -1;
long idx_last = last_o ? (long)(last_o - s) : -1;
long idx_sub = sub ? (long)(sub - s) : -1;
bool c2_pass = (idx_first == 4 && idx_last == 8 && idx_sub == 7 && pbrk != NULL && *pbrk == '1' && spn == 3 && cspn == 3);
char mem[16];
memset(mem, 'X', 6);
mem[6] = '\0';
memcpy(mem, "abcdef", 7);
memmove(mem + 2, mem, 4);
mem[6] = '\0';
int mc_eq = (memcmp(mem, "ababcd", 6) == 0) ? 1 : 0;
const char *mchr = (const char *)memchr(mem, 'c', 6);
long mchr_idx = mchr ? (long)(mchr - mem) : -1;
bool c3_pass = (mc_eq == 1 && mchr_idx == 4);
char csv[] = "red,green;blue";
int tok_cnt = 0;
char joined[32] = {0};
char *tok = strtok(csv, ",;");
while (tok != NULL) {
if (tok_cnt > 0) strncat(joined, "|", sizeof(joined) - strlen(joined) - 1);
strncat(joined, tok, sizeof(joined) - strlen(joined) - 1);
tok_cnt++;
tok = strtok(NULL, ",;");
}
const char *err0 = strerror(0);
bool c4_pass = (tok_cnt == 3 && strcmp(joined, "red|green|blue") == 0 && err0 != NULL && strlen(err0) > 0);
printf(
"{"
"\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
"\"cases\":["
"{\"name\":\"copy_concat_length_compare\",\"expected\":\"len=13,lt=1,ncmp=1\",\"actual\":\"len=%zu,lt=%d,ncmp=%d\",\"passed\":%s},"
"{\"name\":\"search_and_span_functions\",\"expected\":\"o1=4,o2=8,sub=7,spn=3,cspn=3\",\"actual\":\"o1=%ld,o2=%ld,sub=%ld,spn=%zu,cspn=%zu\",\"passed\":%s},"
"{\"name\":\"memory_ops_and_memmove_overlap\",\"expected\":\"val=ababcd,c_idx=4\",\"actual\":\"val=%s,c_idx=%ld\",\"passed\":%s},"
"{\"name\":\"strtok_and_strerror\",\"expected\":\"cnt=3,joined=red|green|blue\",\"actual\":\"cnt=%d,joined=%s\",\"passed\":%s}"
"]"
"}\n",
len, cmp_lt, ncmp_eq, c1_pass ? "true" : "false",
idx_first, idx_last, idx_sub, spn, cspn, c2_pass ? "true" : "false",
mem, mchr_idx, c3_pass ? "true" : "false",
tok_cnt, joined, c4_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-string-h-lab/explore-standard-library-string-h.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-standard-library-string-h.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3258.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip