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
posted @ 2022-12-19 22:13  Fun_with_Words  阅读(208)  评论(0编辑  收藏  举报









 张牌。