aboutsummaryrefslogtreecommitdiff
path: root/replace_digit_char.c
blob: 53f0c8f05e94e4996ea011fe03ca2eaf4c081a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 1844. Replace All Digits with Characters
#include "leetcode.h"

/*
 * given a 0-indexed string 's' that has lowercase english letters in its even
 * indices and digits in its odd indices. there is a function 'shift(x, c)'
 * where 'x' is a character and 'c' is an integer that returns the c'th
 * character after 'x'. for every odd index 'i', you want to replace the digit
 * 's[i]' with shift(s[i - 1], s[i]). return 's' after replacing all digits.
 */

char *replaceDigits(char *s) {
  int n = strlen(s);
  for (int i = 1; i < n; i++)
    s[i] += s[i - 1] - '0';
  return s;
}

int main() {
  char *s1 = "a1c1e1", *s2 = "a1b2c3d4e";
  printf("%s\n", replaceDigits(s1)); // expect: abcdef
  printf("%s\n", replaceDigits(s2)); // expect: abbdcfdhe
}