天天看點

strstr, strcat 函數的實作strstr函數:傳回主串中子字元串的位置後的所有字元。

strstr函數:傳回主串中子字元串的位置後的所有字元。

#include <stdio.h>

const char *my_strstr(const char *str, const char *sub_str)

{

    for(int i = 0; str[i] != '\0'; i++)

    {

        int tem = i; //tem保留主串中的起始判斷下标位置 

        int j = 0;

        while(str[i++] == sub_str[j++])

        {

            if(sub_str[j] == '\0')

            {

                return &str[tem];

            }

        }

        i = tem;

    }

    return NULL;

}

int main()

{

    char *s = "1233345hello";

    char *sub = "345";

    printf("%s\n", my_strstr(s, sub));

    return 0;

}

strcat

(1) 函數原型

char *strcat(char *dest, const char *src);

(2) 函數說明

dest 為目的字元串指針,src 為源字元串指針。strcat() 會将參數 src 字元串複制到參數 dest 所指的字元串尾部;dest 最後的結束字元 NULL 會被覆寫掉,并在連接配接後的字元串的尾部再增加一個 NULL。

注意:dest 與 src 所指的記憶體空間不能重疊,且 dest 要有足夠的空間來容納要複制的字元串。

(3) 傳回值

傳回dest 字元串起始位址。

--------------------- 本文來自 aaronymhe 的CSDN 部落格 ,全文位址請點選:https://blog.csdn.net/yi_ming_he/article/details/74612841?utm_source=copy

strstr, strcat 函數的實作strstr函數:傳回主串中子字元串的位置後的所有字元。
strstr, strcat 函數的實作strstr函數:傳回主串中子字元串的位置後的所有字元。