从制造到创造
软件工程师成长之路
posts - 292,  comments - 96,  trackbacks - 0
字符串替换

  1 
  2 #include <iostream.h>
  3 #include <string.h>
  4 
  5 //////////////////////////////////////////////////////////////
  6 //                                                            //
  7 // 函数名:                                                    //
  8 //                                                            //
  9 //        SearchSubString                                        //
 10 //                                                            //
 11 // 函数功能:                                                //
 12 //                                                            //
 13 //        在源字符串中查找子字符串                            //
 14 //                                                            //
 15 // 入口参数:                                                //
 16 //                                                            //
 17 //        strSrc: 源字符串                                    //
 18 //        strSearch: 子字符串                                    //
 19 //                                                            //
 20 // 返回值:                                                    //
 21 //                                                            //
 22 //        -1:在源字符串没有找到子字符串                        //
 23 //        非-1:子字符串在源字符串中的起始序号                    //
 24 //                                                            //
 25 //////////////////////////////////////////////////////////////
 26 
 27 int SearchSubString( char strSrc[], char strSearch[] )
 28 {
 29     int nSearchIndex = -1;        // 返回值序号
 30 
 31     int nIndex = 0;                // 当前序号
 32     while ( strSrc[nIndex] != '\0' )
 33     {
 34         if ( strSrc[nIndex] == strSearch[0] )    // 当第一个字符相同时
 35         {
 36             // 判断后面的字符是否都相同
 37             for ( unsigned int nPos = 1; nPos < strlen( strSearch ); nPos++ )
 38             {
 39                 if ( strSrc[nIndex + nPos] != strSearch[nPos] )
 40                 {
 41                     break;
 42                 }
 43             }
 44 
 45             if ( nPos == strlen( strSearch ) )        // 如果找到子字符串
 46             {
 47                 nSearchIndex = nIndex;
 48 
 49                 break;
 50             }
 51             else        // 第一个字符串后的字符串不相同,源字符串的查找位置下移
 52             {
 53                 nIndex++;
 54             }
 55         }
 56         else        // 当第一个字符不相同时,源字符串的查找位置下移
 57         {
 58             nIndex++;
 59         }
 60     }
 61 
 62     return nSearchIndex;
 63 }
 64 
 65 //////////////////////////////////////////////////////////////
 66 //                                                            //
 67 // 函数名:                                                    //
 68 //                                                            //
 69 //        ReplaceString                                        //
 70 //                                                            //
 71 // 函数功能:                                                //
 72 //                                                            //
 73 //        在源字符串中替换字符串                                //
 74 //                                                            //
 75 // 入口参数:                                                //
 76 //                                                            //
 77 //        strSrc: 源字符串                                    //
 78 //        strSearch: 被替换的字符串                            //
 79 //        strReplace:替换的字符串                                //
 80 //                                                            //
 81 // 返回值:                                                    //
 82 //                                                            //
 83 //        无返回值                                            //
 84 //                                                            //
 85 // 说明:                                                    //
 86 //                                                            //
 87 //        源字符串是以被替换的字符串开始的字符串                //
 88 //                                                            //
 89 //////////////////////////////////////////////////////////////
 90 
 91 void ReplaceString( char strSrc[], char strSearch[], char strReplace[] )
 92 {
 93     int nSearchLen = strlen( strSearch );    // 被替换的字符串的长度
 94     int nReplaceLen = strlen( strReplace );    // 替换的字符串的长度
 95 
 96     if ( nSearchLen < nReplaceLen )            // 查找字符串比替换字符串短
 97     {
 98         // 移动字符串
 99         for ( unsigned int nIndex = strlen( strSrc ); nIndex >= nSearchLen; nIndex-- )
100         {
101             strSrc[nIndex + nReplaceLen - nSearchLen] = strSrc[nIndex];
102         }
103     }
104     else                                    // 查找字符串比替换字符串长
105     {
106         // 移动字符串
107         int nIndex = nSearchLen;
108         while ( strSrc[nIndex] != '\0' )
109         {
110             strSrc[nIndex - nSearchLen + nReplaceLen] = strSrc[nIndex];
111 
112             nIndex++;
113         }
114         strSrc[nIndex - nSearchLen + nReplaceLen] = '\0';
115     }
116 
117     // 替换字符串
118     for ( int nIndex = 0; nIndex < nReplaceLen; nIndex++ )
119     {
120         strSrc[nIndex] = strReplace[nIndex];
121     }
122 
123     return;
124 }
125 
126 //////////////////////////////////////////////////////////////
127 //                                                            //
128 // 函数名:                                                    //
129 //                                                            //
130 //        ReplaceSubString                                    //
131 //                                                            //
132 // 函数功能:                                                //
133 //                                                            //
134 //        在源字符串中替换字符串                                //
135 //                                                            //
136 // 入口参数:                                                //
137 //                                                            //
138 //        strSrc: 源字符串                                    //
139 //        strSearch: 被替换的字符串                            //
140 //        strReplace:替换的字符串                                //
141 //                                                            //
142 // 返回值:                                                    //
143 //                                                            //
144 //        无返回值                                            //
145 //                                                            //
146 //////////////////////////////////////////////////////////////
147 
148 void ReplaceSubString( char strSrc[], char strSearch[], char strReplace[] )
149 {
150     int nIndex = 0;                // 开始查找的位置
151     int nSearchIndex = 0;        // 在当前字符串中被替换的字符串的位置
152 
153     while ( ( nSearchIndex = SearchSubString( strSrc + nIndex, strSearch ) ) != -1 )
154     {
155         ReplaceString( strSrc + nIndex + nSearchIndex, strSearch, strReplace );
156 
157         nIndex += nSearchIndex + strlen( strReplace );
158     }
159 
160     return;
161 }
162 
163 void main()
164 {
165     char strArr[128= "abcdeGHbcdejHKbcdeOOO";
166     char strSearch[] = "bcde";
167     char strReplace[] = "1111111";
168 
169     ReplaceSubString( strArr, strSearch, strReplace );
170 
171     cout << strArr << endl;
172 
173     return;
174 }

posted on 2006-09-08 19:29 CoderDream 阅读(657) 评论(0)  编辑  收藏 所属分类: 算法

只有注册用户登录后才能发表评论。


网站导航:
 

<2006年9月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(9)

我参与的团队

随笔分类(245)

随笔档案(239)

文章分类(3)

文章档案(3)

收藏夹(576)

友情链接

搜索

  •  

积分与排名

  • 积分 - 456294
  • 排名 - 114

最新评论

阅读排行榜

评论排行榜