This is a test version of Biostars. For the public version, visit https://www.biostars.org.
GC content by c programming

how to find GC content in a given DNA sequence by c programming ? Not in any other language.

c genome gene

1 answer

One way is to read DNA sequence into a char [] or terminated char * and loop over each character:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int
main(int argc, const char** argv)
{
    unsigned int gc = 0;
    const char * sequence = "gtcctttTctaGacAtaNNaggtgggaCat";
    size_t sequence_len = strlen(sequence);

    for (size_t i = 0; i < sequence_len; i++) {
        char residue = (char) toupper(sequence[i]);
        switch (residue) {
            case 'C':
            case 'G':
                gc++;
                break;
            case 'A':
            case 'T':
            case 'N':
            default:
                break;
        }
    }

    fprintf(stdout, "GC content: %f\n", (float) gc / sequence_len);

    return EXIT_SUCCESS;
}

Multiply the fraction by 100 to get a percentage.

Log in to answer this question.