Quick find and replace on strings

Started by
22 comments, last by emileej 20 years, 5 months ago
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]
Emil Johansen- SMMOG AI designerhttp://smmog.com
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?
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.*/
Emil Johansen- SMMOG AI designerhttp://smmog.com
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;}
Emil Johansen- SMMOG AI designerhttp://smmog.com
A suggestion? Use std::string !!!
It will not sacrifice speed? There is a build in function?
Emil Johansen- SMMOG AI designerhttp://smmog.com
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Fruny
It will not sacrifice speed?

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



How sad.

Anyone here who wanna?
Emil Johansen- SMMOG AI designerhttp://smmog.com
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Look - if this is the kinda stuff you intend to post then please leave this thread.
Emil Johansen- SMMOG AI designerhttp://smmog.com

This topic is closed to new replies.

Advertisement