home *** CD-ROM | disk | FTP | other *** search
- /*********
- * Function: CHRSWAP
- * By: Tom Rettig
- * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
- *
- * Syntax: CHRSWAP( <expC>, <char1> [,<char2>] )
- * Return: <expC> with all occurrences of <char1> replaced with <char2>.
- * Note : <char1> and <char2> are both <expC>.
- * <char1>s are simply removed if <char2> is null or omitted.
- *********/
-
- #include "trlib.h"
-
- TRTYPE chrswap()
- {
- static char funcname[] = { "chrswap" };
- char *instr, *replace, *newchar, *ret;
- int i, j;
-
- if ( (PCOUNT==3 && ISCHAR(1) && ISCHAR(2) && ISCHAR(3)) ||
- (PCOUNT==2 && ISCHAR(1) && ISCHAR(2)) )
- {
- instr = _parc(1);
- replace = _parc(2);
- ret = _tr_allocmem( (unsigned)(_tr_strlen(instr)+1));
-
- if ( ret )
- {
- if ( PCOUNT==3 )
- {
- newchar = _parc(3);
- if (! *newchar)
- goto noparam;
- for ( i=0; instr[i]; i++ )
- ret[i] = (instr[i]==replace[0]) ? newchar[0] : instr[i];
- }
- else /* no replacement char */
- {
- noparam:
- for ( i=0, j=0; instr[j]; j++)
- {
- if ( instr[j]!=replace[0] )
- ret[i++] = instr[j];
- }
- }
- ret[i] = NULLC;
-
- _retc( ret );
- _tr_freemem( ret,(unsigned)(_tr_strlen(instr)+1) );
- }
- else
- _retc( _tr_errmsgs(funcname,E_ALLOC) );
- }
- else
- _retc( _tr_errmsgs(funcname,E_SYNTAX) );
- }
-