1+ /**
2+ * This is a very naive implementation of the PHP function `str_replace` in C.
3+ * I wrote it in purpuse to learn C basics.
4+ *
5+ *
6+ * Basically, it search for the index of the searched word in the subject string.
7+ *
8+ * If no index found, then it returns the subject.
9+ *
10+ * Otherwize it splits the subject string into 3 parts :
11+ * - prefix : all chars before the searched word
12+ * - searched word
13+ * - suffix : all chars after the searched word
14+ * and it replaces the searched word by the replace parameter.
15+ *
16+ * Finally, it join prefix with replace and suffix, and returns it as string.
17+ *
18+ * @author BetaWeb / Cyprien GLEPIN
19+ **/
20+
21+ #include "str_replace.h"
22+
23+ /**
24+ * Returns the first index of a character or a group of characters on a string
25+ *
26+ * @param str The string in which to look
27+ * @param c The character or group of characters of which we want to found the index
28+ *
29+ * @return The found index if it exists, -1 otherwise
30+ **/
31+ int indexOf (char * str , char * c ) {
32+ char * pointer = strlen (c ) > 1
33+ ? strstr (str , c )
34+ : strchr (str , c [0 ]);
35+
36+ if (pointer == NULL ) {
37+ return -1 ;
38+ }
39+
40+ return pointer - str ;
41+ }
42+
43+ /**
44+ * Replaces the first occurrence of the searched string in the subject string.
45+ *
46+ * @param search The substring to look for
47+ * @param replace The substring with which to replace the found substring
48+ * @param subject The string in which to look
49+ *
50+ * @return A new string with the search/replacement performed
51+ **/
52+ char * str_replace (char * search , char * replace , char * subject ) {
53+ int suffix_len , result_len , search_len , replace_len , subject_len , index ;
54+ char * result , * prefix , * suffix ;
55+
56+ search_len = strlen (search );
57+ subject_len = strlen (subject );
58+ index = indexOf (subject , search );
59+
60+ if (search_len == 0 || index < 0 ) {
61+ result = malloc (subject_len + 1 );
62+ strncpy (result , subject , subject_len + 1 );
63+
64+ return result ;
65+ }
66+
67+ prefix = malloc (index + 1 );
68+ strncpy (prefix , subject , index );
69+
70+ suffix_len = subject_len - (index + search_len );
71+ suffix = malloc (suffix_len + 1 );
72+ strncat (suffix , subject + index + search_len , suffix_len );
73+
74+ replace_len = strlen (replace );
75+ result_len = strlen (prefix ) + replace_len + suffix_len + 1 ;
76+ result = malloc (result_len + 1 );
77+ snprintf (result , result_len , "%s%s%s" , prefix , replace , suffix );
78+
79+ free (prefix );
80+ free (suffix );
81+
82+ return result ;
83+ }
84+
85+ int main (void ) {
86+ char * original_str , * replaced_str ;
87+
88+ original_str = "This is a very powerful implementation of str_replace function" ;
89+ replaced_str = str_replace ("powerful" , "naive" , original_str );
90+
91+ printf ("Original : %s (%d char)\n" , original_str , strlen (original_str ));
92+ printf ("Replaced : %s (%d char)\n" , replaced_str , strlen (replaced_str ));
93+
94+ free (replaced_str );
95+ original_str = NULL ;
96+
97+ return 1 ;
98+ }
0 commit comments