VB icon

String Replacement (str_replace)

Email
Submitted on: 1/8/2015 1:30:00 PM
By: Brian Folts (from psc cd)  
Level: Advanced
User Rating: By 5 Users
Compatibility: C
Views: 2814
 
     This gives you the ability to use str_replace in C. str_replace is a very powerful function with many applications.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :String Replacement (str_replace)
//**************************************
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: String Replacement (str_replace)
// Description:This gives you the ability to use str_replace in C. str_replace is a very powerful function with many applications.
// By: Brian Folts (from psc cd)
//
// Inputs:First parameter is string to replace.
Second parameter is what to replace with.
Third parameter is the string to do it in.
//
// Returns:Returns the new string with all occurences of first parameter replaced with second parameter.
//**************************************

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//replaces all occurences with another occurence
char *str_replace(char * t1, char * t2, char * t6){
	char*t4;
	char*t5=malloc(0);
	while(strstr(t6,t1)){
		t4=strstr(t6,t1);
		strncpy(t5+strlen(t5),t6,t4-t6);
		strcat(t5,t2);
		t4+=strlen(t1);
		t6=t4;
	}
	return strcat(t5,t4);
}
int main(){
	printf("%s",str_replace("hello","goodbye","hello worldhello hello hello helloworld hello hello hello hello hellohello worldhello hello hello helloworld hello hello hello hello hellohello worldhello hello hello helloworld hello hello hello hello hello,and then it gets to the new line?\n"));
	return 0;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.