C++实现将一个字符串中的字符替换成另一个字符串的方法
本文实例讲述了C++实现将一个字符串中的字符替换成另一个字符串的方法,分享给大家供大家参考。具体方法如下:
题目要求:
原地实现字符串中的每个空格替换成"%20",例如输入"Wearehappy",输出"We%20are%20happy"
被替换的字符串当然不仅仅是空格,上面只是个例子
这是道很好的题目,也是百度面试中的一道题,题目不难,但是问题得考虑全面。这里给出如下实现代码:
#include<iostream> #include<vector> #include<cassert> usingnamespacestd; intfindNumberFirst(constchar*str,constchar*dest,vector<int>&pvec) { if(str==NULL||dest==NULL) return0; intpos=0; intlenStr=strlen(str); intlenDest=strlen(dest); if(lenStr<lenDest) return0; intcount=0; while(pos<=lenStr-lenDest) { if(strncmp(str+pos,dest,strlen(dest))==0) { pvec.push_back(pos); pos+=lenDest; count++; } else { pos++; } } returncount; } intfindNumberLast(constchar*str,constchar*dest,vector<int>&pvec) { if(str==NULL||dest==NULL) return0; intstrLen=strlen(str); intdestLen=strlen(dest); if(strLen<destLen) return0; intpos=0; while(pos<=strLen-destLen) { if(strncmp(str+pos,dest,strlen(dest))==0) { pos+=destLen; pvec.push_back(pos-1); } else { pos++; } } returnpvec.size(); } voidreplaceArray(char*str,constchar*src,constchar*dest) { if(str==NULL||src==NULL||dest==NULL) return; vector<int>pvec; intstrLen=strlen(str); intsrcLen=strlen(src); intdestLen=strlen(dest); if(strLen<srcLen) return; intposBefore=0; intposAfter=0; if(srcLen<destLen) { intcount=findNumberLast(str,src,pvec); if(count<=0) return; posAfter=strLen+count*(destLen-srcLen)-1; posBefore=strLen-1; while(count>0&&posBefore>=0) { if(pvec[count-1]==posBefore) { posAfter-=destLen; strncpy(str+posAfter+1,dest,strlen(dest)); count--; posBefore--; } else { str[posAfter--]=str[posBefore--]; } } } elseif(strLen>destLen) { intcount=findNumberFirst(str,src,pvec); if(count<=0) return; posAfter=0; posBefore=0; inti=0; while(count>=0&&posBefore<strLen) { if(count>0&&pvec[i]==posBefore) { strncpy(str+posAfter,dest,strlen(dest)); posAfter+=destLen; count--; posBefore+=srcLen; i++; } else { str[posAfter++]=str[posBefore++]; } } str[posAfter]='\0'; } } voidmain() { char*str=newchar[100]; if(str==NULL) return; memset(str,'\0',100); constchar*src=""; constchar*dest="%20"; //case1:只有1个空格 strcpy(str,""); replaceArray(str,src,dest); cout<<"str:"<<str<<endl; replaceArray(str,dest,src); cout<<"str:"<<str<<endl; //case2:两个空格 strcpy(str,""); replaceArray(str,src,dest); cout<<"str:"<<str<<endl; replaceArray(str,dest,src); cout<<"str:"<<str<<endl; //case3:正常情况 strcpy(str,"wearehappy"); replaceArray(str,src,dest); cout<<"str:"<<str<<endl; replaceArray(str,dest,src); cout<<"str:"<<str<<endl; //case3:空格在前 strcpy(str,"wearehappy"); replaceArray(str,src,dest); cout<<"str:"<<str<<endl; replaceArray(str,dest,src); cout<<"str:"<<str<<endl; //case4:空格在后 strcpy(str,"wearehappy"); replaceArray(str,src,dest); cout<<"str:"<<str<<endl; replaceArray(str,dest,src); cout<<"str:"<<str<<endl; //case4:没空格 strcpy(str,"wearehappy"); replaceArray(str,src,dest); cout<<"str:"<<str<<endl; replaceArray(str,dest,src); cout<<"str:"<<str<<endl; //case5:两边一样 strcpy(str,"wearehappy"); replaceArray(str,src,dest); cout<<"str:"<<str<<endl; src="%20"; assert(dest=="%20"); replaceArray(str,dest,src); cout<<"str:"<<str<<endl; }
分析上述代码,很有意思的一个情况是srcLen和destLen或大或小的情形,其边界条件的判定不一样。比如wearehappy为例子,从后往前拷贝时,count=2。
在count=0时,恰好将最前面的空格替换完成,we则不用重复拷贝。但是对于从前往后拷贝,当count=0时,最后面的happy将不会被拷贝。
希望本文所述实例对大家C++程序算法设计的学习有所帮助。