Here’s the program:

rna_transcription.c

#include "rna_transcription.h"
#include <malloc.h>
#include <string.h>

static const char lookup[] = {
    ['A'] = 'U',
    ['C'] = 'G',
    ['G'] = 'C',
    ['T'] = 'A'
};

char *to_rna(const char *dna)
{
    if (!dna)
        return NULL;

    char *rna = calloc(strlen(dna) + 1, 1), *start_rna = rna;
    if (rna)
    {
        for (; *dna; dna++, rna++)
        {
            if (!(*rna = lookup[(int)*dna]))
            {
                free(rna);
                return NULL;
            }
        }
    }
    return start_rna;
}

rna_transcription.h

#ifndef RNA_TRANSCRIPTION_H
#define RNA_TRANSCRIPTION_H

char *to_rna(const char *dna);

#endif

I can’t help but wonder how much of a waste of space the array would be. Surely, using a map is better, right?

  • GissaMittJobb@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    3 months ago

    A switch statement is probably a decent option here, yeah. You trade off a little bit of memory for what might be a few more instructions executing the switch statement, unless the compiler picks up on it and optimizes it. Maybe check godbolt for what gets generated in practice if you really care about it.