strcspn详解
请先看输出:
"go" "depth" "10" s=' go depth 10' "go" "depth" "10" s=' go'
学计算机的科班生知道:token - an individual instance of a continuous character sequence without spaces, used in lexical analysis [词法分析]. 之后的stage(级)有语法分析、代码生成等。token的原意是街机用的那种代币。恐怕不是人人都知道strcspn为啥叫这个名字。
程序:
#include <stdio.h> #include <string.h> // "Spans the complement [补集] of charset [字符集] // Returns the number of bytes in 's' of which are not in 'reject'. size_t strcspn(const char *s, const char *reject); size_t strspn(const char *s, const char *accept); const char DELIMITER[] = " \t\r\n"; // White spaces void print_tokens(const char* s) { for (;;) { s += strspn(s, DELIMITER); // 走到第一个非DELIMITER字符 size_t len = strcspn(s, DELIMITER); if (len == 0) break; // https://cplusplus.com/reference/cstdio/printf // The precision is not specified in the format string, but as an additional integer value argument preceding // the argument that has to be formatted. printf("\"%.*s\"\n", len, s); s += len; } } void print_tokens_2(char *s) { for (char* tk = strtok(s, DELIMITER); tk; tk = strtok(NULL, DELIMITER)) printf("\"%s\"\n", tk); } int main() { //char s[] = "a bb ccc"; char s[] = " \tgo \tdepth \t \t10"; print_tokens(s); printf("s='%s'\n\n", s); print_tokens_2(s); printf("s='%s'\n", s); getchar(); return 0; }
再来一点:
char *strsep(char **stringp, const char *delim); // The strsep() function was introduced as a replacement for strtok, since the latter cannot handle empty fields. // However, strtok conforms to C89/C99 and hence is more portable. // https://linux.die.net/man/3/strsep char *strpbrk(const char *s, const char *accept); // "string pointer break" // Returns a pointer to the byte in s that matches one of the bytes in accept, or NULL if no such byte is found. // https://www.man7.org/linux/man-pages/man3/strpbrk.3.html // 好像没啥用。宽字符版还能对付下"炮 二平 五"和"炮3进 1",把数字挑出来? // The strcoll() function compares the two strings s1 and s2. he comparison is based on strings interpreted as // appropriate for the program's current locale for category LC_COLLATE. 'a'比'A'大,'九'和'0'(全角字符)呢? // Collation means the reading of two (or more) texts side-by-side in order to note their differences.
// Proofreading means reading a proof copy of a text in order to detect and correct any errors.
// proof: trial copy of printed material produced so that corrections may be made 校样
size_t strxfrm(char *restrict dest, const char *restrict src, size_t n); // The strxfrm() function transforms the src string into a form such that the result of strcmp(3) on two strings // that have been transformed with strxfrm() is the same as the result of strcoll on the two strings before their // transformation
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
2021-12-19 百度网盘是如何检测是否安装了客户端的?
2021-12-19 A Child's History of England.59
2021-12-19 EDA学习笔记
2021-12-19 Stockfish和GNU Chess棋力都很强
2021-12-19 PyTorch与张量简介