Skip to content
Snippets Groups Projects
Unverified Commit 21a046f9 authored by Bachir Lachguel's avatar Bachir Lachguel
Browse files

Add libstring stuff !

parent a3902176
No related branches found
No related tags found
No related merge requests found
......@@ -38,6 +38,31 @@ int main(void) {
}
string_print(string_final);
free(string_final);
/**********************************************************************/
string_t *original = string_new("Hello world");
string_t *substring_old = string_new("world");
string_t *substring_new = string_new("Paris");
if (!original || !substring_old || !substring_new) {
printf("Invalid strings before substitution !");
return -2;
}
string_print(original);
string_t *new = string_replace(original, substring_old, substring_new);
if (!new) {
printf("Invalid strings after substitution !");
return -3;
}
free(original);
free(substring_old);
free(substring_new);
string_print(new);
free(new);
return 0;
}
......@@ -288,9 +313,14 @@ string_t *string_replace(const string_t *str, const string_t *old,
return string_clone(str);
size_t len = str->len + n * (new->len - old->len);
string_t *s = malloc(sizeof(string_t) + len);
// We add this check
if (!s) {
return s;
}
s->len = len;
char *t = s->buf;
int curr = 0;
offset = 0;
while (n--) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment