int replaces=FindAndReplace(char *str,char *search,char *replace);
"On a long enough timeline the survival rate of everyone drops to zero"
- Fight Club
[edited by - emileej on November 15, 2003 8:01:25 AM]
Quick find and replace on strings
How is this done the fastest? Is there a standard function I missed which will do the job fast? What I am lookin for it a function like this:
strstr()
finds an occurance of a substring in a string, returning the offset of the substring in the string if it finds one.Replacing it would be another story because you might need to resize the string. I wonder if
std::string
has a library for this?
Hey I could use some of you optimization freaks in here... The functions needs to be run quite a number of times doin a lot of replacing.
More specified function:
More specified function:
int FindAndReplace(char *str,const char *search,const char *replace);//...char *str="Hey you fuckhead I am so fucking tired of your fucking my fucking girlfriend.";printf("Replaces: %d Result: %s",FindAndReplace(str,"fuck","OOPS"),str);/* Output should be: Replaces: 4 Result: Hey you OOPShead I am so OOPSing tired of your OOPSing my OOPSing girlfriend.*/
Heres an attempt that failed (Got failed assert when running the code):
int strreplace(char *str,const char *search,const char *replace){ int found=0; for(int i=0,j;i<strlen(str);i++){ for(j=0;j<strlen(search);j++) if(str[i+j]!=search[j]) break; if(j==strlen(search)){ found++; int startlen=i,endlen=strlen(str)-i-strlen(search),newlen=startlen+strlen(replace)+endlen; char *first=(char*)malloc(sizeof(char)*startlen); memcpy(first,str,sizeof(char)*startlen); char *second=(char*)malloc(sizeof(char)*endlen); memcpy(second,str+i+strlen(search),sizeof(char)*endlen); realloc(str,sizeof(char)*newlen+1); memcpy(str,first,sizeof(char)*startlen); free(first); memcpy(str+startlen,replace,sizeof(char)*strlen(replace)); memcpy(str+startlen+strlen(replace),second,endlen); free(second); } } return found;}
It will not sacrifice speed?
*sigh* I won''t even comment on that.
There is a build in function?
std::string::find and std::string::replace.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
*sigh* I won''t even comment on that.
There is a build in function?
std::string::find and std::string::replace.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
quote:Original post by Fruny
It will not sacrifice speed?
*sigh* I won''t even comment on that.
How sad.
Anyone here who wanna?
quote:Original post by emileej
Anyone here who wanna?
The greatest performance gain you can ever get when programming is when you turn a non-working application into a working one.
Time for you to learn how to program. Use the C++ library.
(yes, I''m tired of debunking the same myths over and over again.)
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement