DNA核苷酸计数
问题描述: 给定一行核苷酸序列,长度最长为1000 nt, 返回其中'A', 'T', 'C', 'G'出现的次数
C代码如下:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "dbg.h"
#define MAX_SIZE 1001
int main( int argc, char *argv[])
{
check(argc == 2, "Need an argument");
FILE *filein ;
if( ( filein = fopen(argv[1], "r")) == NULL){
printf("unable to open %s \n", argv[1]);
return 1;
}
char nucl[MAX_SIZE];
char *in = NULL;
in = fgets(nucl, MAX_SIZE - 1, filein);
check(in != NULL, "Failed to read file");
// count
int count =0;
char letter;
int A_NUM = 0, T_NUM = 0, C_NUM = 0 , G_NUM = 0;
for ( count = 0; nucl[count] != '\0' && nucl[count] != '\n'; count++){
letter = toupper(nucl[count]);
switch(letter){
case 'A':
A_NUM ++;
break;
case 'T':
T_NUM ++;
break;
case 'C':
C_NUM ++;
break;
case 'G':
G_NUM ++;
break;
default:
printf("%d: %c is not a valid base\n", count, letter);
}
}
printf("A\tC\tG\tT\n");
printf("%d\t%d\t%d\t%d\t\n",A_NUM, C_NUM, G_NUM, T_NUM);
fclose(filein);
return 0;
error:
return -1;
}
DNA转录成RNA
问题描述: RNA由A, C, G, U组成,其中U是对应DNA编码链上的T。给定一段编码链上的序列,将其翻译成RNA序列。
C代码如下:
#include <stdio.h>
#include <ctype.h>
#include "dbg.h"
#define LINE_MAX 80
int main(int argc, char *argv[])
{
check(argc == 2, "USAGES: ./RNA file");
char *filename = argv[1];
FILE *in = fopen(filename, "r");
check(in != NULL, "unable to open file");
char line[LINE_MAX];
int i = 0;
while ( (fgets(line, LINE_MAX - 1, in)) != NULL){
for (i = 0; line[i] != '\0'; i++){
int base = toupper(line[i]);
if ( base == 'T'){
printf("%c", 'U');
} else{
printf("%c", base);
}
}
}
printf("\n");
return 0;
error:
return -1;
}