zoukankan      html  css  js  c++  java
  • C 标准库

    strstr

    • Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
    • 查找 substr 所指的空终止字节字符串在 str 所指的空终止字节字符串中的首次出现。不比较空终止字符。
    • 若 str 或 substr 不是指向空终止字节字符串的指针,则行为未定义。
    char *strstr(const char *haystack, const char *needle)
    

    Parameters

    haystack

    • C string to be scanned.
    • 要被检索的 C 字符串。

    needle

    • C string containing the sequence of characters to match.
    • 在 haystack 字符串内要搜索的小字符串。

    Return Value

    • A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
    • 该函数返回在 haystack 中第一次出现 needle 字符串的位置,如果未找到则返回 null。

    Example

    //
    // Created by zhangrongxiang on 2017/8/15 14:15.
    // Copyright (c) 2017 ZRC . All rights reserved.
    //
    
    //#include <syslib.h>
    #include <string.h>
    #include <stdio.h>
    
    //C 库函数 char *strstr(const char *haystack, const char *needle) 在字符串 haystack 中查找第一次出现字符串 needle 的位置,不包含终止符 ''。
    
    void find_str(char const *str, char const *substr) {
        char *pos = strstr(str, substr);
        if (pos) {
            printf("found the string '%s' in '%s' at position: %ld; result: %s
    ", substr, str, pos - str, pos);
        } else {
            printf("the string '%s' was not found in '%s'
    ", substr, str);
        }
    }
    
    int main() {
        char *s = "GoldenGlobalView";
        char *l = "lob";
        char *p;
        //返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;
        // 如果str2不是str1的子串,则返回NULL。
        p = strstr(s, l);
        if (p)
            //lobalView
            printf("%s
    ", p);
        else
            printf("NotFound!
    ");
    
        char *str = "one two three";
        find_str(str, "two"); //found the string 'two' in 'one two three' at position: 4; result: two three
        find_str(str, ""); // found the string '' in 'one two three' at position: 0; result: one two three
        find_str(str, "nine");// the string 'nine' was not found in 'one two three'
        find_str(str, "n"); // found the string 'n' in 'one two three' at position: 1; result: ne two three
    
        char str2[] = "This is a simple string";
        char *pch;
        pch = strstr(str2, "simple");
        strncpy (pch, "sample", 6);
        puts(str2);//This is a sample string
        puts(pch);//sample string
    
        return 0;
    }
    

    文章参考

    转载注明出处

  • 相关阅读:
    发现一个github上特别优秀的面试准备资料
    坠吼的大哥的博客
    [BJDCTF 2nd]假猪套天下第一
    [网鼎杯 2020 朱雀组]phpweb
    [GWCTF 2019]我有一个数据库
    [BJDCTF2020]ZJCTF,不过如此
    [GXYCTF2019]禁止套娃
    洛谷
    [ZJCTF 2019]NiZhuanSiWei
    蓝帽杯决赛-爆炒腰花-WP
  • 原文地址:https://www.cnblogs.com/zhangrxiang/p/8419384.html
Copyright © 2011-2022 走看看