strtok,strspn、strcspn和strpbrk

Defined in header  <string.h>
  
char *strtok( char          *str, const char          *delim );
  (until C99)
char *strtok( char *restrict str, const char *restrict delim );
  (since C99)
     

Finds the next token in a null-terminated byte string pointed to by str. The separator characters are identified by null-terminated byte string pointed to by delim.

This function is designed to be called multiples times to obtain successive tokens from the same string.

  • If str != NULL, the call is treated as the first call to strtok for this particular string. The function searches for the first character which is not contained in delim.
  • If no such character was found, there are no tokens in str at all, and the function returns a null pointer.
  • If such character was found, it is the beginning of the token. The function then searches from that point on for the first character that is contained in delim.
  • If no such character was found, str has only one token, and future calls to strtok will return a null pointer
  • If such character was found, it is replaced by the null character '\0' and the pointer to the following character is stored in a static location for subsequent invocations.
  • The function then returns the pointer to the beginning of the token
  • If str == NULL, the call is treated as a subsequent calls to strtok: the function continues from where it left in previous invocation. The behavior is the same as if the previously stored pointer is passed as str.

Parameters

str-pointer to the null-terminated byte string to tokenize
delim-pointer to the null-terminated byte string identifying delimiters

Return value

Pointer to the beginning of the next token or NULL if there are no more tokens.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    char *str = (char*)malloc(20);
    char *tok = NULL;
    int len = 0;

    strcpy(str, "This is a string");
    len = strlen(str);

    printf("string before strtok(): %s\n", str);
    tok = strtok(str, " ");
    while (tok) {
        printf("Token: %s\n", tok);
        tok = strtok(NULL, " ");
    }

    printf("Print mangled string after strtok()\n");
    for (int i = 0; i < len; i++) {
        if (str[i] == '\0') {
            printf("'\\0'");
        } else {
            printf("%c", str[i]);
        }
    }
    printf("\n");
    free(str);

    return 0;
}

输出为

string before strtok(): This is a string
Token: This
Token: is
Token: a
Token: string
Print mangled string after strtok()
This'\0'is'\0'a'\0'string

Defined in header  <string.h>
  
size_t strspn( const char *dest, const char *src );
   
     

Returns the length of the maximum initial segment of the byte string pointed to by dest, that consists of only the characters found in byte string pointed to by src.

Parameters

dest-pointer to the null-terminated byte string to be analyzed
src-pointer to the null-terminated byte string that contains the characters to search for

Return value

The length of the maximum initial segment that contains only characters from byte string pointed to by src

strspn返回dest中包含src连续段最长的个数

#include <string.h>
#include <stdio.h>
 
int main(void)
{
    char *string_find = "abcde312$#@";
    char *low_alpha = "qwertyuiopasdfghjklzxcvbnm";
    printf("%zu", strspn(string_find, low_alpha));
    return 0;
}

输出为

5


Defined in header  <string.h>
  
char* strpbrk( const char* dest, const char* breakset );
   
     

Scans the null-terminated byte string pointed to by dest for any character from the null-terminated byte string pointed to by breakset, and returns a pointer to that character.

Parameters

dest-pointer to the null-terminated byte string to be analyzed
breakset-pointer to the null-terminated byte string that contains the characters to search for

Return value

Pointer to the first character in dest, that is also in breakset, or null pointer if no such character exists.

strpbrk返回dest中第一次出现breakset中字符的指针

#include <stdio.h>
#include <string.h>
 
int main(void)
{
    const char* str = "hello world, friend of mine!";
    const char* sep = " ,!";
 
    unsigned int cnt = 0;
    do {
       str = strpbrk(str, sep); // find separator
       if(str) str += strspn(str, sep); // skip separator
       ++cnt; // increment word count
    } while(str && *str);
 
    printf("There are %d words\n", cnt);
}

输出为

There are 5 words


Defined in header  <string.h>
  
size_t strcspn( const char *dest, const char *src );
   
     

Returns the length of the maximum initial segment of the byte string pointed to by dest, that consists of only the characters not found in byte string pointed to by src.

Parameters

dest-pointer to the null-terminated byte string to be analyzed
src-pointer to the null-terminated byte string that contains the characters to search for

Return value

The length of the maximum initial segment that contains only characters not found in the byte string pointed to by src

Example

#include <string.h>
#include <stdio.h>
 
int main(void)
{
    char *string_find = "abcdefg**";
    char *character = "*";
    printf("%zu", strcspn(string_find, character));
 
    return 0;
}

Output:

7






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值