aboutsummaryrefslogtreecommitdiff
path: root/add_space_string.c
blob: 3d6bf04b5727de5826abbd993c3d5e307deb6321 (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
28
29
30
31
32
33
34
35
36
37
38
39
// 2109. Adding Spaces to a String
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * given a 0-indexed string 's' and a 0-indexed integer array 'spaces' that
 * describes the indices in the original string where spaces will be added. each
 * space should be inserted before the character at the given index. return the
 * modified string after the spaces have been added
 */

char *addSpaces(char *s, int *spaces, int spaces_size) {
  int n = strlen(s), m = n + 1 + spaces_size;
  char *ans = malloc(m * sizeof(char));
  ans[m - 1] = '\0';
  int aidx = 0, bidx = 0;
  int *num = malloc(spaces_size * sizeof(int));
  num[0] = spaces[0];
  for (int i = 1; i < spaces_size; i++)
    num[i] = spaces[i] - spaces[i - 1];
  for (int i = 0; i < spaces_size; i++) {
    strncpy(&ans[aidx], &s[bidx], num[i]);
    aidx += num[i];
    bidx += num[i];
    ans[aidx] = ' ';
    aidx++;
  }
  strncpy(&ans[aidx], &s[bidx], m - aidx);
  free(num);
  return ans;
}

int main() {
  char s1[] = {"LeetcodeHelpsMeLearn"}, s2[] = {"icodeinpython"};
  int n1[] = {8, 13, 15}, n2[] = {1, 5, 7, 9};
  printf("%s\n", addSpaces(s1, n1, 3)); // expect: Leetcode Helps Me Learn
  printf("%s\n", addSpaces(s2, n2, 4)); // expect: i code in py thon
}