From 321825828ac918bad28d0597a8616c6dc9802c3c Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 11 Aug 2021 15:26:15 +0530 Subject: Add solved exercises --- c/nucleotide-count/src/nucleotide_count.c | 30 ++++++++++++++++++++++++++++++ c/nucleotide-count/src/nucleotide_count.h | 8 ++++++++ 2 files changed, 38 insertions(+) create mode 100644 c/nucleotide-count/src/nucleotide_count.c create mode 100644 c/nucleotide-count/src/nucleotide_count.h (limited to 'c/nucleotide-count/src') 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 +#include + +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 + +char *count(const char *dna_strand); + +#endif -- cgit 1.4.1-2-gfad0