• <em id="6vhwh"><rt id="6vhwh"></rt></em>

    <style id="6vhwh"></style>

    <style id="6vhwh"></style>
    1. <style id="6vhwh"></style>
        <sub id="6vhwh"><p id="6vhwh"></p></sub>
        <p id="6vhwh"></p>
          1. 国产亚洲欧洲av综合一区二区三区 ,色爱综合另类图片av,亚洲av免费成人在线,久久热在线视频精品视频,成在人线av无码免费,国产精品一区二区久久毛片,亚洲精品成人片在线观看精品字幕 ,久久亚洲精品成人av秋霞

            常用字符串源代碼

            更新時間:2023-12-11 18:57:48 閱讀: 評論:0

            2023年12月11日發(fā)(作者:玉珠集團)

            -

            常用字符串源代碼

            常用字符串源代碼

            1、strstr

            strstr函數(shù)有兩個版本:

            [cpp]

            1.

            2.

            const

            char * strstr (

            const

            char * str1,

            const

            char * str2 );

            char * strstr ( char * str1,

            const

            char * str2 );

            (1) 樸素的實現(xiàn)方式

            遍歷兩個字符串,在str1中逐個匹配str2,時間復(fù)雜度O(nm).

            (2) KMP算法

            strstr的兩種實現(xiàn)參考文章:

            2、strlen

            以下兩種實現(xiàn)類似,后一種沒有借助局部length變量。

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            11.

            12.

            13.

            14.

            size_t strlen1(

            const

            char *str) {

            asrt(str != NULL);

            unsigned int length = 0;

            while

            ((*str++) != '0')

            ++length;

            return

            length;

            }

            size_t strlen2(

            const

            char *str) {

            asrt(str != NULL);

            const

            char *end = str;

            while

            (*end++) ;

            return

            ((int)(end - str - 1));

            }

            3、strcat strncat

            注意幾點:注意幾點

            a. 給源字符加上const屬性;

            b. 給源地址和目的地址加非零斷言;

            c. 為了實現(xiàn)鏈?zhǔn)讲僮鳎瑢⒛康牡刂贩祷兀皇欠祷豽oid;

            d. 考慮源目的區(qū)域有重疊的情況;

            e. 一定要保證追加操作完后,目的地址最后以空字符'0‘結(jié)尾。

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            11.

            12.

            13.

            14.

            15.

            16.

            17.

            18.

            19.

            class

            ="cpp">char *strcat1(char *destination,

            const

            char *source) {

            asrt (destination != NULL && source != NULL);

            char *cp = destination;

            while

            (*cp)

            ++cp;

            while

            (*cp++ = *source++) ;

            return

            destination;

            }

            char* strncat1(char *destination,

            const

            char *source, size_t count) {

            asrt (destination != NULL && source != NULL);

            char *cp = destination;

            while

            (*cp)

            ++cp;

            while

            (count-- && *source != '0')

            *cp++ = *source++;

            *cp = '0';

            return

            destination;

            }

            4、strcmp strncmp

            注意:下面字符做減法時,要強制類型轉(zhuǎn)換,將char轉(zhuǎn)換為unsigned char,因為strcmp函數(shù)是按照ASCII碼進行比較的,而ASCII碼的范圍是0 ~注意

            255,char的范圍是-127 ~ 127,所以當(dāng)輸入為負(fù)數(shù)時會返回錯誤。

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            11.

            12.

            13.

            14.

            15.

            16.

            17.

            18.

            19.

            20.

            21.

            22.

            23.

            24.

            25.

            26.

            27.

            28.

            29.

            30.

            31.

            32.

            int strcmp1(

            const

            char *str1,

            const

            char *str2) {

            asrt(str1 != NULL && str2 != NULL);

            int result = 0;

            while

            ( !(result = *(unsigned char*)str1 - *(unsigned char*)str2) && *str2) {

            ++str1;

            ++str2;

            }

            if

            (result < 0)

            return

            -1;

            el

            if

            (result > 0)

            return

            1;

            return

            result;

            }

            int strncmp1(

            const

            char *str1,

            const

            char *str2, size_t count) {

            asrt(str1 != NULL && str2 != NULL);

            int result = 0;

            //下一行必須將count--寫在前邊,否則count等于0時還會計算一個ret

            while

            (count-- && (!(result = *(unsigned char*)str1 - *(unsigned char*)str2)) && *str2) {

            ++str1;

            ++str2;

            }

            if

            (result < 0)

            return

            -1;

            el

            if

            (result > 0)

            return

            1;

            return

            result;

            }

            5、strcpy strncpy

            注意幾點:注意幾點

            a. 給源字符加上const屬性;

            b. 給源地址和目的地址加非零斷言;

            c. 為了實現(xiàn)鏈?zhǔn)讲僮鳎瑢⒛康牡刂贩祷兀皇欠祷豽oid;

            d. 考慮源目的區(qū)域有重疊的情況;

            e. 一定要保證復(fù)制完后,目的地址最后以空字符'0‘結(jié)尾。[cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            11.

            12.

            13.

            14.

            15.

            16.

            17.

            18.

            19.

            20.

            21.

            22.

            23.

            24.

            char *strcpy(char *destination,

            const char *source) {

            asrt(destination != NULL && source != NULL);

            if (destination == source)

            return destination;

            char *cp = destination;

            while ((*cp++ = *source++) != '0')

            ;

            return destination;

            }

            char *strncpy1(char *destination,

            const char *source, size_t count) {

            asrt(destination != NULL && source != NULL);

            if (destination == source)

            return destination;

            char *cp = destination;

            while (count-- && *source != '0')

            *cp++ = *source++;

            *cp = '0';

            return destination;

            }

            6、strpbrk有兩個版本:[cpp]

            1.

            2.

            const char * strpbrk (

            const char * str1,

            const char * str2 );

            char * strpbrk ( char * str1,

            const char * str2 );

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            11.

            12.

            13.

            14.

            15.

            16.

            17.

            18.

            19.

            20.

            /*

            Returns a pointer to the first occurrence in str1 of any of the

            characters that are part of str2, or a null pointer if there are no matches.

            The arch does not include the terminating null-characters

            該函數(shù)也是兩個版本:const和非const版本

            */

            char *strpbrk1(char *str1,

            const

            char *str2) {

            asrt((str1 != NULL) && (str2 != NULL));

            const

            char *s;

            while

            (*str1 != '0') {

            s = str2;

            while

            (*s != '0'){

            if

            (*str1 == *s)

            return

            str1;

            ++ s;

            }

            ++ str1;

            }

            return

            NULL;

            }

            7、memcpy

            該函數(shù)不檢查source結(jié)尾的null字符,僅僅拷貝count個字節(jié)。為了避免溢出,destination和source指針?biāo)傅臄?shù)組必須最少有count個字節(jié),而且

            兩個區(qū)域不能重疊。

            如果區(qū)域有重疊,那么要使用memmove這個更安全的方式。

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            11.

            void

            *memcpy1(

            void

            *destination,

            const

            void

            *source, size_t count) {

            asrt (destination != NULL && source != NULL);

            void

            *address = destination;

            while

            (count--) {

            *(char*)destination = *(char*)source;

            destination = (char *)destination + 1;

            source = (char *)source + 1;

            }

            return

            address;

            }

            8、memmove

            和memcpy函數(shù)一樣,該函數(shù)也不會檢查source末尾的空字符null,僅僅拷貝count個字節(jié);為了避免溢出,destination和source指針?biāo)傅臄?shù)組必

            須最少有count個字節(jié)。但是該函數(shù)允許源和目的該函數(shù)允許源和目的區(qū)域重疊。

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            11.

            12.

            13.

            14.

            15.

            16.

            17.

            void

            *memmove1(

            void

            *destination,

            const

            void

            *source, size_t count) {

            asrt (destination != NULL && source != NULL);

            char *pdest = (char*)destination;

            char *psrc = (char*)source;

            //pdest在psrc后面,且兩者距離小于count,從尾部開始移動,

            //其他情況從頭部開始移動

            if

            ((pdest > psrc) && (pdest - psrc < count)) {

            while

            (count--)

            *(pdest + count) = *(psrc + count);

            }

            el

            {

            while

            (count--)

            *pdest++ = *psrc++;

            }

            return

            destination;

            }

            9、memt

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            11.

            void

            *memt1(

            void

            *str, int value, size_t count) {

            if

            (str == NULL)

            return

            NULL;

            void

            *p = str;

            while

            (count--) {

            *(char*)p = (char)value;

            p = (char *)p + 1;

            }

            return

            str;

            }

            10、strchr memchr

            memchr函數(shù)功能:查找在num字節(jié)內(nèi),value(解釋為unsigned char)第一次出現(xiàn)的位置,返回指向它的指針。

            兩個版本:

            [cpp]

            1.

            2.

            const

            void

            * memchr (

            const

            void

            * ptr, int value, size_t num );

            void

            * memchr (

            void

            * ptr, int value, size_t num );

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            9.

            10.

            void

            *memchr1(

            void

            *str, int value, size_t count) {

            if

            (str == NULL)

            return

            NULL;

            while

            (count--) {

            if

            (*(char*)str == value)

            return

            (

            void

            *)str;

            str = (char*)str + 1;

            }

            return

            NULL;

            }

            strchr也有兩個版本:

            [cpp]

            1.

            2.

            const

            char * strchr (

            const

            char * str, int character );

            char * strchr ( char * str, int character );

            [cpp]

            1.

            2.

            3.

            4.

            5.

            6.

            7.

            8.

            //查找字符串s中首次出現(xiàn)字符c的位置

            char *strchr1(char *str, int c) {

            asrt(str != NULL);

            for

            (; *str != (char)c; ++ str)

            if

            (*str == '0')

            return

            NULL;

            return

            str;

            }

            -

            常用字符串源代碼

            本文發(fā)布于:2023-12-11 18:57:43,感謝您對本站的認(rèn)可!

            本文鏈接:http://www.newhan.cn/zhishi/a/1702292267243245.html

            版權(quán)聲明:本站內(nèi)容均來自互聯(lián)網(wǎng),僅供演示用,請勿用于商業(yè)和其他非法用途。如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除。

            本文word下載地址:常用字符串源代碼.doc

            本文 PDF 下載地址:常用字符串源代碼.pdf

            標(biāo)簽:目的   返回   函數(shù)   地址   字符   區(qū)域
            留言與評論(共有 0 條評論)
               
            驗證碼:
            Copyright ?2019-2022 Comsenz Inc.Powered by ? 實用文體寫作網(wǎng)旗下知識大全大全欄目是一個全百科類寶庫! 優(yōu)秀范文|法律文書|專利查詢|
            主站蜘蛛池模板: 欧美精品在线观看视频| 国模精品一区二区三区| 国产亚洲一二三区精品| 国产精品一区二区久久| 亚洲国产一区二区A毛片| 妓女妓女一区二区三区在线观看 | 9久9久热精品视频在线观看| 色综合一本到久久亚洲91| 国产精品中文字幕久久| 天堂影院一区二区三区四区| 蜜桃av一区二区高潮久久精品| 国产精品亚洲片在线| 亚洲国产精品综合一区二区| 免费无码高H视频在线观看| 日本亚洲色大成网站www久久| 亚洲国产v高清在线观看| 欧美成人在线免费| 久久亚洲精品11p| 91丝袜美腿高跟国产老师在线| 国产精品自拍实拍在线看| 久久久久久久综合日本| 91蜜臀国产自产在线观看| 午夜福利日本一区二区无码| 国产短视频一区二区三区| 国产精品中文av专线| 麻豆成人av不卡一二三区| 99热久久只有这里是精品| 国产偷国产偷亚洲高清人| 毛片久久网站小视频| 日韩成人无码影院| 亚洲av成人三区国产精品| 国产日韩av二区三区| 中文字幕在线不卡一区二区| 在线无码免费的毛片视频| 日本一区二区三区黄色| 中文字幕乱妇无码AV在线| 国产精品国产精品一区精品| 亚洲精品日本一区二区| 亚洲第一香蕉视频啪啪爽| 亚洲免费观看一区二区三区| 国产主播一区二区三区|