aboutsummaryrefslogtreecommitdiff
path: root/challenge-102/abigail/c/ch-2.c
blob: 328cb2bd61b3d339e630331423edbe34a161d795 (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
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <stdbool.h>

/*
 * See ../README.md
 */

/*
 * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
 */

# define MAX_LENGTH 23

/*
 * We generate the string to be created backwards, using an
 * array 'out'. Here we store either a '-1' if the string needs
 * a hash in that position, or the index otherwise.
 * 
 * At the end, we print the pieces in reverse, mapping -1 to a hash.
 */

int main (void) {
    long long index;
    char tmp [MAX_LENGTH];

    while (scanf ("%lld", &index) > 0) {
        /*
         * Create an array of long longs. (More than we need).
         */
        long long * out;
        if ((out = (long long *) malloc (index * sizeof (long long))) == NULL) {
            perror ("Malloc 'out' failed");
            exit (1);
        }

        bool hash = false;
        size_t i = 0;

        while (index) {
            if ((hash = !hash)) {
                out [i] = -1;
                index --;
            }
            else {
                out [i] = index + 1;
                sprintf (tmp, "%lld", out [i]);
                index -= strnlen (tmp, MAX_LENGTH);
            }
            i ++;
        }
        for (;i --;) {
            if (out [i] == -1) {
                printf ("#");
            }
            else {
                printf ("%lld", out [i]);
            }
        }
        printf ("\n");

        free (out);
    }
    return (0);
}