Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created July 21, 2018 04:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Silva97/25c606b18bd15c56b7177e3a04dae532 to your computer and use it in GitHub Desktop.
Save Silva97/25c606b18bd15c56b7177e3a04dae532 to your computer and use it in GitHub Desktop.
/********************
* Exemplo de replace em C.
* Por Luiz Felipe.
* https://github.com/Silva97
********************/
#include <stdio.h>
#include <string.h>
int replace(char *text, char *word, char *new);
int main(){
char text[20] = "Apenas um teste.";
replace(text, "um", "dois");
puts(text);
return 0;
}
int replace(char *text, char *word, char *new){
char *start;
int sizew = strlen(word),
sizen = strlen(new);
start = strstr(text, word);
if(!start)
return 0;
strcpy(start + sizen, start + sizew);
memmove(start, new, sizen);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment