strstr strtok

1, strstr strcasestr

From Linux man ( version 1.6d )

这个比较简单,如下面描述:

#include <string.h>
char *strstr(const char *haystack, const char *needle);
char *strcasestr(const char *haystack, const char *needle);

DESCRIPTION
The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating `\0’ characters are not compared.

The strcasestr() function is like strstr(), but ignores the case of both arguments.

RETURN VALUE
These functions return a pointer to the beginning of the substring, or NULL if the substring is not found.

strstr返回haystack中的第一个needle位置,如果没有找到needle,则返回NULL。

2, strtok strtok_r

#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);

strtok_r是strtok的可重入实现,这两个函数用下面的例子说明:

注意:代码来自于strtok的man手册,稍有改动。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int
 6 main(int argc, char *argv[])
 7 {
 8    char *str1, *str2, *token, *subtoken;
 9    char *saveptr1, *saveptr2;
10    int j;
11 
12    if (argc != 4) {
13        fprintf(stderr, "Usage: %s string delim subdelim\n",
14                argv[0]);
15        exit(EXIT_FAILURE);
16    }
17 
18    for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
19         token = strtok_r(str1, argv[2], &saveptr1);
20         if (token == NULL)
21            break;
22         printf("%d: %s\r\n", j, token);
23 
24         for (str2 = token; ; str2 = NULL) {
25             subtoken = strtok_r(str2, argv[3], &saveptr2);
26             if (subtoken == NULL)
27                 break;
28             printf("\t--> %s\r\n", subtoken);
29         }
30    }
31 
32    exit(EXIT_SUCCESS);
33 } /* main */

编译运行说明:

gcc test.c

(root:)[test]:)./a.out "hello.c  hello.o  libhello.a  main.c  malloc.c  syslog.c  test.  test.c  test.i  time.c  time.h" " " "."

下面是运行结果:

1: hello.c
        --> hello
        --> c
2: hello.o
        --> hello
        --> o
3: libhello.a
        --> libhello
        --> a
4: main.c
        --> main
        --> c
5: malloc.c
        --> malloc
        --> c
6: syslog.c
        --> syslog
        --> c
7: test.
        --> test
8: test.c
        --> test
        --> c
9: test.i
        --> test
        --> i
10: time.c
        --> time
        --> c
11: time.h
        --> time
        --> h

分析:

输入:

"hello.c  hello.o  libhello.a  main.c  malloc.c  syslog.c  test.  test.c  test.i  time.c  time.h" " " "."

分隔符是空格 " ", 子分隔符是 "."

将"hello.c  hello.o  libhello.a  main.c  malloc.c  syslog.c  test.  test.c  test.i  time.c  time.h" 以空格分隔,出现11个字段。

每个字段再以 "." 分隔,出现 "-->“ 的显示,如  -->time -->h。

 

 

 

 

 

转载于:https://www.cnblogs.com/marty1001/p/3580850.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值