aboutsummaryrefslogtreecommitdiff
path: root/capitalise_the_title.c
blob: 06575cd6482f78670517ad5d99485f02c5c9b2b4 (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
40
41
42
43
44
// 2129. Capitalize the Title
#include "leetcode.h"

/*
 * given a string 'title' consisting of one or more words separated by a single
 * space. where each word consists of english letters. capitalise the string by
 * changing the capitalisation of each word such that if the length of the word
 * is 1 or 2 letters, change all letters to lowercase. otherwise,change the
 * first letter to uppercase and the remaining letters to lowercase. return the
 * capitalised title.
 */

void process(char *dest, char *src) {
  int n = strlen(src);
  if (n < 3)
    for (int i = 0; i < n; i++)
      dest[i] = tolower(src[i]);
  else {
    dest[0] = toupper(src[0]);
    for (int i = 1; i < n; i++)
      dest[i] = tolower(src[i]);
  }
}

char *capitalizeTitle(char *title) {
  int n = strlen(title), begin = 0;
  char *ans = (char *)malloc((n + 1) * sizeof(char));
  ans[n] = '\0';
  for (int i = 0; i < n; i++)
    if (title[i] == ' ') {
      title[i] = '\0';
      process(&ans[begin], &title[begin]);
      ans[i] = ' ';
      begin = i + 1;
    }
  process(&ans[begin], &title[begin]);
  return ans;
}

int main() {
  char *t1 = "capiTalIze tHe titLe", *t2 = "First leTTeR of EACH Word",
       *t3 = "i lOve leetcode";
  char *ct1 = capitalizeTitle(t1);
}