summary refs log tree commit diff stats
path: root/c/nucleotide-count/src/nucleotide_count.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/nucleotide-count/src/nucleotide_count.c')
-rw-r--r--c/nucleotide-count/src/nucleotide_count.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/c/nucleotide-count/src/nucleotide_count.c b/c/nucleotide-count/src/nucleotide_count.c
new file mode 100644
index 0000000..7cd8c79
--- /dev/null
+++ b/c/nucleotide-count/src/nucleotide_count.c
@@ -0,0 +1,30 @@
+#include "nucleotide_count.h"
+#include <math.h>
+#include <stdio.h>
+
+struct DNA {
+    size_t a;
+    size_t c;
+    size_t g;
+    size_t t;
+};
+
+char *count(const char *dna_strand) {
+    struct DNA strand = {0};
+
+    for (size_t idx = 0; dna_strand[idx] != '\0'; idx++) {
+        if (dna_strand[idx] == 'A') strand.a++;
+        else if (dna_strand[idx] == 'C') strand.c++;
+        else if (dna_strand[idx] == 'G') strand.g++;
+        else if (dna_strand[idx] == 'T') strand.t++;
+        else return calloc(1, sizeof(char)); // Invalid strand.
+    }
+
+    unsigned int str_size = 1 + snprintf(NULL, 0, "A:%zu C:%zu G:%zu T:%zu",
+                                         strand.a, strand.c, strand.g, strand.t);
+    char *strand_count = calloc(str_size, sizeof(char));
+    snprintf(strand_count, str_size, "A:%zu C:%zu G:%zu T:%zu",
+             strand.a, strand.c, strand.g, strand.t);
+
+    return strand_count;
+}