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

            Feedback

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

            久久久久国产| 天堂无码久久综合东京热| 久久久久久久人妻无码中文字幕爆 | www.久久热| 久久久精品日本一区二区三区| 久久婷婷色香五月综合激情| 午夜欧美精品久久久久久久| 四虎国产永久免费久久| 久久国产欧美日韩精品| 9191精品国产免费久久| 欧美一区二区三区久久综合| 久久青青草原精品国产不卡| 国内精品久久久人妻中文字幕| 开心久久婷婷综合中文字幕| 久久久一本精品99久久精品66| 蜜桃麻豆www久久国产精品| 国产亚洲色婷婷久久99精品| 国产A级毛片久久久精品毛片| 久久艹国产| 久久精品亚洲男人的天堂| 久久精品无码专区免费东京热 | 青青草国产精品久久久久| 伊人久久大香线蕉亚洲五月天| 精品久久人人爽天天玩人人妻| 国产精品9999久久久久| 久久久久久久人妻无码中文字幕爆| 日本五月天婷久久网站| 久久久久无码专区亚洲av| 青青草原1769久久免费播放| 久久精品二区| 成人综合伊人五月婷久久| 久久国产三级无码一区二区| 99久久久精品免费观看国产 | 久久亚洲美女精品国产精品| 久久91精品国产91| 久久精品日日躁夜夜躁欧美 | 国产精品无码久久综合网| 韩国三级中文字幕hd久久精品| 精品久久久久久久中文字幕| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久精品国产69国产精品亚洲|