aboutsummaryrefslogtreecommitdiff
path: root/score_of_string.c
blob: c415f9e0b146c3749c1948d9edc9a8b40e0ba44e (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
// 3110. Score of a String
#include "leetcode.h"

/*
 * you are given a string 's'. the score of a string is defined as the sume of
 * the absolute different between the ascii values of adjacent characters.
 * return the score of 's'.
 */

int scoreOfString(char *s) {
  int ans = 0, val = 0, n = strlen(s);
  while (--n) {
    val = *s - *(s + 1);
    if (val < 0)
      val = -val;
    ans += val;
    s++;
  }
  return ans;
}

int main() {
  char *s1 = "hello", *s2 = "zaz";
  printf("%d\n", scoreOfString(s1)); // expect: 13
  printf("%d\n", scoreOfString(s2)); // expect: 50
}