• <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://m.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)秀范文|法律文書|專利查詢|
            主站蜘蛛池模板: 丁香婷婷激情综合俺也去| 亚洲最大福利视频网| 人人人澡人人肉久久精品| 精品黄色av一区二区三区| 亚洲熟女乱色综合亚洲图片| 亚洲大尺度无码无码专线| 久久精品国内一区二区三区| 噜噜噜噜私人影院| 国产成人 综合 亚洲欧洲 | 国产不卡免费一区二区| 久久久久亚洲A√无码| 亚洲成AV人片在线观高清| 中文字幕日韩有码一区| 中文字幕丰满乱子无码视频| 国产人妇三级视频在线观看| 亚洲国产青草衣衣一二三区| 成人免费AA片在线观看 | 久久国产精品精品国产色婷婷| 老色99久久九九爱精品| 亚洲男女内射在线播放| 国产精品视频全国免费观看| 国产黄色大片一区精品| 成人性生交片无码免费看| 国产精品一品二区三四区| 日本亚洲一级中文字幕| 91精品国产午夜福利| 精品女同一区二区三区不卡| 国产精品无码专区| 无码专区 人妻系列 在线| 精品无码人妻| 久久嫩草影院免费看| 极品蜜桃臀一区二区av| 国产熟睡乱子伦视频在线播放| 国产AV一区二区三区| 日本亚洲成人中文字幕| 亚洲免费日韩一区二区| 91精品国产蜜臀在线观看| 久久精品国产色蜜蜜麻豆| 一区二区三区精品偷拍| 亚洲αⅴ无码乱码在线观看性色| 亚洲精品v欧美精品动漫精品|