aboutsummaryrefslogtreecommitdiff
path: root/challenge-010/zapwai/c/replace.h
blob: 4083bdc3476ffbc12e1095193bc9bc78c97409c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef REPLACE_H
#define REPLACE_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* replace needle with noodle, returns a char* you should free afterward. */
char* replace(char* haystack, char* needle, char* noodle) {
  int wlen = strlen(haystack) + strlen(noodle);
  char* loc = strstr(haystack, needle);
  if (loc == NULL)
    return NULL;
  int start_pt = strlen(haystack) - strlen(loc);
  char* post = malloc(wlen*sizeof(char));
  strcpy(post, &haystack[start_pt + strlen(needle)]);
  char* pre = malloc(wlen*sizeof(char));
  strncpy(pre, haystack, start_pt);
  char* ans = malloc(wlen*sizeof(char));
  strcat(ans, pre);
  strcat(ans, noodle);
  strcat(ans, post);
  free(pre);
  free(post);
  return ans;
}

#endif