• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              8 Posts :: 0 Stories :: 16 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(3)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            實現這個函數:
            char* strrep(const char* src,  const char* from, const char* to)
            {
            }

            將src中出現的所有from替換成to
            時間:
            10分鐘。

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



            #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 左言 閱讀(2361) 評論(1)  編輯 收藏 引用

            Feedback

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

            久久国产精品久久国产精品| 色欲久久久天天天综合网| 国产精品久久亚洲不卡动漫| 97久久精品国产精品青草| 国产精品久久久天天影视香蕉| 久久综合久久综合亚洲| 久久亚洲AV成人无码电影| 日本福利片国产午夜久久| 久久久久亚洲AV无码专区网站| 久久午夜伦鲁片免费无码| 久久久久国产精品麻豆AR影院 | 99国内精品久久久久久久| 无码任你躁久久久久久久| 无码日韩人妻精品久久蜜桃| 久久精品女人天堂AV麻| 国产精品久久久久影院嫩草| 久久免费看黄a级毛片| 精品国产青草久久久久福利 | 久久综合给合综合久久| 久久久精品国产sm调教网站| 久久精品国产亚洲5555| 狠狠狠色丁香婷婷综合久久五月| 久久久黄色大片| 婷婷久久综合| 丁香五月综合久久激情| 精品999久久久久久中文字幕| 欧美丰满熟妇BBB久久久| 久久久久亚洲AV片无码下载蜜桃| 精品一久久香蕉国产线看播放| 国产亚洲精品自在久久| 久久精品人成免费| 久久天天躁狠狠躁夜夜96流白浆| 国产精品一区二区久久精品涩爱| 久久久久无码精品| 欧美麻豆久久久久久中文| 久久久国产精品| 色偷偷91久久综合噜噜噜噜| 欧美精品福利视频一区二区三区久久久精品 | 色8久久人人97超碰香蕉987| 人妻丰满AV无码久久不卡| 天天爽天天狠久久久综合麻豆|