How to Assign a Digital Cypher in C


The challenge

Digital Cypher assigns to each letter of the alphabet unique number. For example:

 a  b  c  d  e  f  g  h  i  j  k  l  m
 1  2  3  4  5  6  7  8  9 10 11 12 13
 n  o  p  q  r  s  t  u  v  w  x  y  z
14 15 16 17 18 19 20 21 22 23 24 25 26

Instead of letters in encrypted word we write the corresponding number, eg. The word scout:

 s  c  o  u  t
19  3 15 21 20

Then we add to each obtained digit consecutive digits from the key. For example. In case of key equal to 1939 :

   s  c  o  u  t
  19  3 15 21 20
 + 1  9  3  9  1
 ---------------
  20 12 18 30 21
  
   m  a  s  t  e  r  p  i  e  c  e
  13  1 19 20  5 18 16  9  5  3  5
+  1  9  3  9  1  9  3  9  1  9  3
  --------------------------------
  14 10 22 29  6 27 19 18  6  12 8

Task

Write a function that accepts str string and key number and returns an array of integers representing encoded str.

Input / Output

The str input string consists of lowercase characters only.
The key input number is a positive integer.

Example

Encode("scout",1939);  ==>  [ 20, 12, 18, 30, 21]
Encode("masterpiece",1939);  ==>  [ 14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8]

The solution in C

Option 1:

#include <stdlib.h>
#include <string.h>
#include <math.h>

unsigned char *encode(const char *s, unsigned k)
{
    unsigned char *enc, c;
    unsigned n, ncyph, cyph, key;
    if (!s || !(enc = malloc(sizeof(unsigned char) * strlen(s))))
        return NULL;
    ncyph = pow(10, (unsigned)log10(k));
    for (n=cyph=0; (c = *(s+n)); ++n) {
         if (!cyph) {
            cyph = ncyph;
            key = k;
        }
        *(enc+n) = c - 'a' + 1 + key / cyph;
        key %= cyph;
        cyph /= 10;
    }
    return enc;
}

Option 2:

#include <stdlib.h>
#include <string.h>

unsigned char *encode(const char *s, unsigned k)
{
    char kbuf[64];
    sprintf(kbuf, "%d", k);
    unsigned char *ret = malloc(strlen(s));
    
    for (char *p = s, *q = kbuf, *r = ret; *p; p++, q++, r++) 
      *r = *p -'a' + 1 + *(*q ? q : (q = kbuf)) - '0';
    
    return ret;
}

Option 3:

#include <stdlib.h>
#include <string.h>
#include <math.h>

unsigned char *encode(const char *s, unsigned k) {

  char key[10], *code = malloc (strlen(s) * sizeof(char));
  int i, next = 0;

  sprintf (key,"%i",k);

  for (i = 0; s[i] != NULL; ++i) {
    if (key[next] == NULL) next = 0;

    code[i] = (s[i] - 'a' + 1) + (key[next++] - '0');
  }
  return code;
}

Test cases to validate our solution

#include <criterion/criterion.h>
#include <stdlib.h>

unsigned char *encode(const char *s, unsigned k);

typedef enum {
    ASSERT_PASS,
    ASSERT_FAIL
} assertop;

assertop assert_mem_eq(const void *actual, const void *expected, size_t n, size_t size)
{
    unsigned long i;
    if (!actual || !expected)
        return actual != expected ? ASSERT_FAIL : ASSERT_PASS;
    for (i=0u; n--; i += size)
        if (memcmp((char *)actual+i, (char *)expected+i, size))
            return ASSERT_FAIL;
    return ASSERT_PASS;
}

Test(Sample_Test, should_return_the_encoded_string)
{
    cr_assert(ASSERT_PASS == assert_mem_eq(encode("scout", 1939), (unsigned char[]){20, 12, 18, 30, 21}, 5, sizeof(unsigned char)));
    cr_assert(ASSERT_PASS ==
        assert_mem_eq(encode("masterpiece", 1939), (unsigned char[]){14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8}, 11, sizeof(unsigned char)));
}