Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions solutions/c/nucleotide-count/1/nucleotide_count.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "nucleotide_count.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char *count(const char *dna_strand) {
uint32_t a, c, g, t;
a = c = g = t = 0;

for (int i = 0; dna_strand[i] != '\0'; i++) {
switch (dna_strand[i]) {
case 'A': a++; break;
case 'C': c++; break;
case 'G': g++; break;
case 'T': t++; break;
default:
return strdup("");
}
}

char *result = malloc(BUFSIZE * sizeof(char));
snprintf(
result, BUFSIZE,
"A:%" PRIu32 " C:%" PRIu32 " G:%" PRIu32 " T:%" PRIu32,
a, c, g, t
);

return result;
}
14 changes: 14 additions & 0 deletions solutions/c/nucleotide-count/1/nucleotide_count.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef NUCLEOTIDE_COUNT_H
#define NUCLEOTIDE_COUNT_H

/* 4 nucleotides
* 10*4 char uint32 representation
* 2*4 char name and separator
* 4-1 char spaces
* 1 char string terminator
*/
#define BUFSIZE 64

char *count(const char *dna_strand);

#endif