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?
A few things come to mind:
O(1)
dna
is really large. Streaming the result or overwriting the presumably already malloc'd input would be the only thing I would touch, and only if I could prove that it improves performance in practice.That is not my program actually. Here's what I've come up with:
rna_transcription.c
#include "rna_transcription.h" static char transcribe_nucleotide(char nucleotide) { switch (nucleotide) { case 'G': return 'C'; case 'C': return 'G'; case 'T': return 'A'; case 'A': return 'U'; default: return nucleotide; } } char *to_rna(const char *dna) { size_t len = strlen(dna); char *rna = malloc((len + 1) * sizeof(char)); for (size_t i = 0; i <= len; ++i) { rna[i] = transcribe_nucleotide(dna[i]); } return rna; }
rna_transcription.h
I could not find the equivalent of map in standard library, so that is why I was interested in the community solutions.
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.