summary refs log tree commit diff stats
path: root/c/nucleotide-count/src
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-08-11 15:26:15 +0530
committerAndinus <andinus@nand.sh>2021-08-11 15:26:15 +0530
commit321825828ac918bad28d0597a8616c6dc9802c3c (patch)
tree0b8e9cb1012197750eb58e972736319b2a6abac2 /c/nucleotide-count/src
parent2979ef790ac5b8f58495e0dd08cafd6a3a2e30a5 (diff)
downloadexercism-321825828ac918bad28d0597a8616c6dc9802c3c.tar.gz
Add solved exercises
Diffstat (limited to 'c/nucleotide-count/src')
-rw-r--r--c/nucleotide-count/src/nucleotide_count.c30
-rw-r--r--c/nucleotide-count/src/nucleotide_count.h8
2 files changed, 38 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;
+}
diff --git a/c/nucleotide-count/src/nucleotide_count.h b/c/nucleotide-count/src/nucleotide_count.h
new file mode 100644
index 0000000..c8775a1
--- /dev/null
+++ b/c/nucleotide-count/src/nucleotide_count.h
@@ -0,0 +1,8 @@
+#ifndef NUCLEOTIDE_COUNT_H
+#define NUCLEOTIDE_COUNT_H
+
+#include <stdlib.h>
+
+char *count(const char *dna_strand);
+
+#endif