home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / STRINGS.ZIP / MVSTR.C < prev    next >
Text File  |  1993-01-14  |  1KB  |  46 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "strings.h"
  4.  
  5. /***
  6.  *  Function    :  strmvstr
  7.  *
  8.  *  Description :  Copy an input string in an output string
  9.  *           with replacing all occurences of a target string.
  10.  *
  11.  *  Parameters  :  out      char  *out_str    result
  12.  *                 in       char  *in_str     in string
  13.  *                 in       char  *target     target string to replace
  14.  *                 in       char  *new_str    string to put in place of target
  15.  *
  16.  *  Return code :  pointer to result.
  17.  *
  18.  *  OS/Compiler :  All
  19.  ***/ 
  20.       
  21. char *strmvstr( char *out_str, const char *in_str, const char *target, const char *new_str )
  22.  
  23. { char *ptr = out_str;
  24.   const char *ptr_new = new_str;
  25.   unsigned count;
  26.  
  27.   for ( count = 0; *out_str = *in_str; out_str ++, in_str ++ )
  28.       if ( *out_str == *target ) { target ++; count ++;
  29.                          if ( ! *target )
  30.                           { out_str -= count - 1;
  31.                             target -= count;
  32.                             count = 0;
  33.                             while ( *out_str++ = *new_str++ );
  34.                         new_str = ptr_new;
  35.                         out_str --; out_str --;
  36.                       }
  37.                  }
  38.                 else { target -= count;
  39.                         out_str -= count;
  40.                    in_str -= count;
  41.                    count = 0;
  42.                  }
  43.  
  44.   return ptr;
  45. }
  46.