summaryrefslogtreecommitdiff
path: root/format.h
blob: 2936e85ad68b3c0d90cf5a0c3e8b6b13f536a34c (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* format.h
 * ----------------------------------------------------------------------------
 * 'THE BEER-WARE LICENSE' (Revision 42):
 * <benjamin@chausse.xyz> wrote this file. As long as you retain this notice
 * you can do whatever you want with this stuff. If we meet some day, and you
 * think this stuff is worth it, you can buy me a beer in return.
 * Benjamin Chausse
 * ----------------------------------------------------------------------------
 */

#define TRUE 1
#define FALSE 0

#define COUNT_OF( arr) (sizeof(arr)/sizeof(0[arr]))

// Conversion table between uppercase and lowercase
const char caps[2][26] = {
  {'A','B','C','D','E','F','G','H','I','J','K','L','M',
    'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'},
  {'a','b','c','d','e','f','g','h','i','j','k','l','m',
    'n','o','p','q','r','s','t','u','v','w','x','y','z'}
};

double pow(double b, int e){
  if (e==1){
    return b;
  } else if (e%2==0){
    return pow(b*b,e/2);
  } else {
    return b * pow(b*b, e/2);
  }
}

// Convert every character to lowercase
const char* lower(char *str){
  int len = COUNT_OF(str);
  for (int i=0; i<len; i++){
    // Change all letters to lowercase
    for (int j=0;j<26;j++){
      if (str[i] == caps[0][j]) {
        str[i] = caps[1][j];
      }
    }
  }
  return str;
}

// Count the total number of spaces to define the new str size
const char* trim(char *str){
  int spc =0;
  for (int i=0; i<COUNT_OF(str); i++){
    if (str[i] == ' '){
      spc++;
    }
  }
  // Actually format the new string
  char *nstr = "";
  int j = 0; // index inside the new str (nstr)
  for (int i=0; i<COUNT_OF(str); i++){
    if (str[i] != ' '){
      nstr[j] = str[i];
      j++;
    }
  }
  nstr[COUNT_OF(nstr)-1] = '\0';
  return nstr;
}

double abs(double i){
  return (i>0) ? i : -i;
}