Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

emileej

Quick find and replace on strings

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
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]

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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:

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.
*/

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
It will not sacrifice speed?

*sigh* I won''t even comment on that.




How sad.

Anyone here who wanna?

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!