README.md

December 5, 2022 ยท View on GitHub

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.

More Info

First parameter is string to replace.

Second parameter is what to replace with.

Third parameter is the string to do it in.

Returns the new string with all occurences of first parameter replaced with second parameter.

Submitted On
ByBrian Folts
LevelAdvanced
User Rating2.8 (14 globes from 5 users)
CompatibilityC
CategoryStrings
WorldC / C++
Archive File

API Declarations

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

Source Code

#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;
}