Quantcast
Channel: YGC » ComputerScience
Viewing all articles
Browse latest Browse all 27

Complementing a Strand of DNA

$
0
0
In DNA strings, symbols 'A' and 'T' are complements of each other, as are 'C' and 'G'.

The reverse complement of a DNA string s is the string sc formed by reversing the symbols of s, then taking the complement of each symbol (e.g., the reverse complement of "GTCA" is "TGAC").

Given: A DNA string s of length at most 1000 bp.

Return: The reverse complement sc of s.

Sample Dataset

AAAACCCGGT
Sample Output

ACCGGGTTTT

读取文件时,每个碱基做互补碱基的mapping,存入时使用反向,也就是后面的碱基存在链表的前面。最后只需遍历链表,把结果打印出来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
#include<stdlib.h>
 
typedef struct ntNode {
  char NT; /* nucleotide */
  struct ntNode *next;
} ntNode;
 
int main() {
  FILE *INFILE;
  INFILE = fopen("../DATA/rosalind_revc.txt", "r");
 
  ntNode *head, *curr;
  head= NULL;
 
  char nt;
  while ( (nt = fgetc(INFILE)) != EOF) {
    curr = malloc(sizeof(ntNode));
 
    switch(nt) {
    case 'A':
      nt = 'T';
      break;
    case 'C':
      nt = 'G';
      break;
    case 'G':
      nt = 'C';
      break;
    case 'T':
      nt = 'A';
      break;
    default:
      nt = ' ';
    }
 
    curr->NT = nt;
    curr->next = head;
    head = curr;
  }
 
  curr = head;
  while(curr) {
    printf("%c", curr->NT);
    curr = curr->next;
  }
  printf("\n");
  return 0;
}

Related Posts


Viewing all articles
Browse latest Browse all 27

Trending Articles