Provides functionality to map one set of characters into another within strings.
More...
#include "mdb.h"
#include <ctype.h>
Go to the source code of this file.
|
char * | replace_chars (char *s, char *from, char *to) |
| Maps one set of characters to another in a given string.
|
|
Provides functionality to map one set of characters into another within strings.
- Copyright
- (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
- (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
- License
- This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
- Author
- M. Borland, C. Saunders, R. Soliday
Definition in file replace_chars.c.
◆ replace_chars()
char * replace_chars |
( |
char * | s, |
|
|
char * | from, |
|
|
char * | to ) |
Maps one set of characters to another in a given string.
This function replaces each character in the string s
that appears in the from
string with the corresponding character in the to
string. If the to
string is shorter than the from
string, it pads the to
string with spaces. Similarly, if the from
string is shorter than the to
string, it pads the from
string with spaces.
- Parameters
-
s | The string to be modified. |
from | The set of characters to be replaced. |
to | The set of characters to replace with. |
- Returns
- The modified string
s
with characters replaced.
Definition at line 32 of file replace_chars.c.
33{
34 long lt, lf;
35 char *ptr, *ptr_to, *ptr_from;
36
37 if ((lt = strlen(to)) < (lf = strlen(from))) {
38 char *to_temp;
39 to_temp =
tmalloc(
sizeof(*to_temp) * (lf + 1));
41 for (; lt < lf; lt++)
42 to_temp[lt] = ' ';
43 to_temp[lf] = 0;
44 } else if (lt > lf) {
45 char *from_temp;
46 from_temp =
tmalloc(
sizeof(*from_temp) * (lt + 1));
48 for (; lf < lt; lf++)
49 from_temp[lf] = ' ';
50 from_temp[lt] = 0;
51 }
52
53 ptr = s;
54 while (*ptr) {
55 ptr_to = to;
56 ptr_from = from;
57 while (*ptr_from)
58 if (*ptr_from != *ptr) {
59 ptr_from++;
60 ptr_to++;
61 } else {
62 *ptr = *ptr_to;
63 break;
64 }
65 ptr++;
66 }
67 return (s);
68}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.