SDDSlib
Loading...
Searching...
No Matches
replace_string.c File Reference

Provides functions to replace occurrences of substrings within strings. More...

#include "mdb.h"

Go to the source code of this file.

Functions

int replace_string (char *t, char *s, char *orig, char *repl)
 Replace all occurrences of one string with another string.
 
int replace_stringn (char *t, char *s, char *orig, char *repl, long count_limit)
 Replace a limited number of occurrences of one string with another string.
 
int replaceString (char *t, char *s, char *orig, char *repl, long count_limit, long here)
 Replace occurrences of one string with another string with additional options.
 

Detailed Description

Provides functions to replace occurrences of substrings within strings.

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_string.c.

Function Documentation

◆ replace_string()

int replace_string ( char * t,
char * s,
char * orig,
char * repl )

Replace all occurrences of one string with another string.

This function replaces all instances of the substring orig in the source string s with the substring repl, and stores the result in the destination string t.

Parameters
tDestination string where the result is stored.
sSource string in which replacements are to be made.
origThe substring to be replaced.
replThe substring to replace with.
Returns
The number of replacements made.

Definition at line 29 of file replace_string.c.

29 {
30 return replaceString(t, s, orig, repl, -1, 0);
31}
int replaceString(char *t, char *s, char *orig, char *repl, long count_limit, long here)
Replace occurrences of one string with another string with additional options.

◆ replace_stringn()

int replace_stringn ( char * t,
char * s,
char * orig,
char * repl,
long count_limit )

Replace a limited number of occurrences of one string with another string.

This function replaces up to count_limit instances of the substring orig in the source string s with the substring repl, and stores the result in the destination string t.

Parameters
tDestination string where the result is stored.
sSource string in which replacements are to be made.
origThe substring to be replaced.
replThe substring to replace with.
count_limitThe maximum number of replacements to perform.
Returns
The number of replacements made.

Definition at line 46 of file replace_string.c.

46 {
47 return replaceString(t, s, orig, repl, count_limit, 0);
48}

◆ replaceString()

int replaceString ( char * t,
char * s,
char * orig,
char * repl,
long count_limit,
long here )

Replace occurrences of one string with another string with additional options.

This function replaces instances of the substring orig in the source string s with the substring repl, up to a maximum of count_limit replacements. The here parameter controls additional replacement behavior. The result is stored in the destination string t.

Parameters
tDestination string where the result is stored.
sSource string in which replacements are to be made.
origThe substring to be replaced.
replThe substring to replace with.
count_limitThe maximum number of replacements to perform. If negative, no limit is applied.
hereAdditional parameter to control replacement behavior.
Returns
The number of replacements made.

Definition at line 65 of file replace_string.c.

65 {
66 char *ptr0, *ptr1;
67 int count;
68 char temp;
69
70 ptr0 = s;
71 t[0] = 0;
72 count = 0;
73 while ((count_limit < 0 || count < count_limit) && (ptr1 = str_in(ptr0, orig))) {
74 if (here && ptr1 != ptr0)
75 break;
76 count++;
77 temp = *ptr1;
78 *ptr1 = 0;
79 strcat(t, ptr0);
80 strcat(t, repl);
81 ptr0 = ptr1 + strlen(orig);
82 *ptr1 = temp;
83 }
84 if (strlen(ptr0))
85 strcat(t, ptr0);
86 return (count);
87}
char * str_in(char *s, char *t)
Finds the first occurrence of the substring t in the string s.
Definition str_in.c:29