SHARE
TWEET

Untitled

a guest Mar 26th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. char* str_replace(char* dst, int num, const char* str,
  2.                   const char* orig, const char* rep){
  3.     const char* ptr;
  4.     size_t len1 = strlen(orig);
  5.     size_t len2 = strlen(rep);
  6.     char*  tmp  = dst;
  7.    
  8.     num -= 1;
  9.     while((ptr = strstr(str, orig)) != NULL){
  10.         num -= (ptr - str) + len2;
  11.         if(num < 1)
  12.             break;
  13.  
  14.         strncpy(dst, str, (size_t)(ptr - str));
  15.         dst += ptr - str;
  16.         strncpy(dst, rep, len2);
  17.         dst += len2;
  18.         str  = ptr + len1;
  19.     }
  20.  
  21.     for(; (*dst = *str) && (num > 0); --num){
  22.         ++dst;
  23.         ++str;
  24.     }
  25.     return tmp;
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top