• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            qinzuoyan

              C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              8 Posts :: 0 Stories :: 16 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(3)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            實(shí)現(xiàn)這個(gè)函數(shù):
            char* strrep(const char* src,  const char* from, const char* to)
            {
            }

            將src中出現(xiàn)的所有from替換成to
            時(shí)間:
            10分鐘。

            要求:
            1. 功能正確,盡量高效
            2.不能調(diào)用現(xiàn)有的正則表達(dá)式庫(kù)
            3.可以使用strdup,malloc,realloc等函數(shù),以及其他的c字符串 操作函數(shù)
            4.估算你所寫算法的時(shí)間復(fù)雜度



            #include 
            <string.h>
            #include 
            <malloc.h>

            /**
             * On success, strrep() returns a newly allocated string, which is constructed by replacing all substring `from' in `src' with `to'.
             * If `from' is a null string(with length of 1), then return a duplication of `src'.
             *
             * On failure
             *   ENOMEM Insufficient memory is available.
             * 
             * src is a null-terminated string.
             * from is a null-terminated string.
             * to is a null-terminated string.
             * 
             
            */
            char* strrep(const char* src,  const char* from, const char* to)
            {
              
            char *des, *des_cur;
              
            int from_len, to_len, src_len, des_len;
              
            const char *src_cur, *src_end, *occ;
              
            const char **marks; 
              
            int mark_len, m, rep_count;
              
            // prepare
              from_len = strlen(from);
              to_len 
            = strlen(to);
              src_len 
            = strlen(src);
              
            if (from_len == 0)
                
            return strdup(src);
              
            // mark all occurence of `from' in `src'
              mark_len = 0x4;
              marks 
            = (const char**)malloc(sizeof(char** mark_len);
              
            if (marks == NULL)
                
            return NULL;
              rep_count 
            = 0;
              src_cur 
            = src;
              
            while((occ = strstr(src_cur, from)) != NULL) {
                rep_count
            ++;
                
            // need more space for mark
                if (rep_count > mark_len) {
                  mark_len 
            << 1;
                  marks 
            = (const char**)realloc(marks, mark_len);
                  
            if (marks == NULL)
                    
            return NULL;
                }
                
            // mark the position
                marks[rep_count - 1= occ;
                
            // find next occurence from the current position
                src_cur = occ + from_len;
              }
              
            // construct new string
              des_len = src_len + (to_len - from_len) * rep_count;
              des 
            = (char*)malloc(des_len + 1);
              
            if (des == NULL)
                
            return NULL;
              des_cur 
            = des;
              src_cur 
            = src;
              m 
            = 0;
              
            if (m < rep_count)
                occ 
            = marks[m];
              
            else
                occ 
            = NULL;
              
            while(*src_cur) {
                
            if (src_cur != occ)
                  
            *des_cur++ = *src_cur++;
                
            else {
                  
            // replace `from' with `to'
                  strncpy(des_cur, to, to_len);
                  src_cur 
            += from_len;
                  des_cur 
            += to_len;
                  
            // more to replace?
                  m++;
                  
            if (m < rep_count)
                    occ 
            = marks[m];
                  
            else
                    occ 
            = NULL;
                }
              }
              des_cur 
            = '\0';
              free(marks);
              
            return des;
            }


            posted on 2010-06-10 12:16 左言 閱讀(2362) 評(píng)論(1)  編輯 收藏 引用

            Feedback

            # re: 一道筆試題 - strrep()函數(shù)的實(shí)現(xiàn)[未登錄] 2010-06-11 10:58 expter
            KMp不就行了嗎?  回復(fù)  更多評(píng)論
              


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产精品免费久久久久影院| 午夜久久久久久禁播电影| 国产精品美女久久久免费| 久久久久四虎国产精品| 国产精品成人久久久久久久| 伊人久久五月天| 97久久久久人妻精品专区| 久久久精品视频免费观看| 亚洲伊人久久大香线蕉综合图片| 97久久天天综合色天天综合色hd | 国产精品99久久99久久久| 欧美亚洲另类久久综合| 一日本道伊人久久综合影| 久久电影网2021| 影音先锋女人AV鲁色资源网久久| 久久综合九色综合97_久久久| 久久这里的只有是精品23| 91精品国产色综合久久| 国产一区二区久久久| 国产国产成人久久精品| 久久精品国产久精国产思思| 亚洲国产成人精品91久久久| 久久免费精品视频| 国产人久久人人人人爽| 久久婷婷午色综合夜啪| 久久露脸国产精品| 国産精品久久久久久久| 国产一区二区三区久久| 久久精品午夜一区二区福利 | 亚洲国产欧洲综合997久久| 久久青青国产| 久久久久亚洲精品中文字幕| 色综合久久88色综合天天| 大伊人青草狠狠久久| 91精品国产色综合久久| 国产精品久久久久影院色| 潮喷大喷水系列无码久久精品| 久久国产精品成人片免费| 久久久婷婷五月亚洲97号色| 久久精品九九亚洲精品| 久久99精品久久只有精品|