-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparatorlib.c
More file actions
49 lines (42 loc) · 1.49 KB
/
comparatorlib.c
File metadata and controls
49 lines (42 loc) · 1.49 KB
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
/*
* forwardscale / backwardscale
* ----------------------------
* These functions take an input string and "shift" each character.
*
* forwardscale:
* output[i] = input[i] + (i + 1)
*
* backwardscale:
* output[i] = input[i] - (i + 1)
*
* Example with forwardscale("hello"):
* 'h' + 1 = 'i'
* 'e' + 2 = 'g'
* 'l' + 3 = 'o'
* 'l' + 4 = 'p'
* 'o' + 5 = 't'
*
* No standard library is required. NULL-terminated strings only.
*/
void forwardscale(const char *in, char *out) {
int i = 0; /* index counter */
while (in[i] != '\0') { /* process until we hit the null terminator */
char c = in[i]; /* read current character */
char shift = (char)(i + 1);
/* amount to shift forward */
out[i] = c + shift; /* apply the shifting rule */
i++; /* move to next character */
}
out[i] = '\0'; /* write null terminator to output */
}
void backwardscale(const char *in, char *out) {
int i = 0; /* index counter */
while (in[i] != '\0') { /* loop until string ends */
char c = in[i]; /* current input char */
char shift = (char)(i + 1);
/* how much to shift backward */
out[i] = c - shift; /* apply reverse shifting */
i++; /* increment index */
}
out[i] = '\0'; /* null-terminate output */
}