简体   繁体   English

C中的短函数-不明白怎么了

[英]short function in c - don't understand what's wrong

I'm writing a function that just calculates the "complementary" strand of DNA, meaning replaces C with G, T with A, and so on. 我正在编写一个函数,该函数仅计算DNA的“互补”链,这意味着将C替换为G,将T替换为A,依此类推。

this is what I wrote: 这是我写的:

#include <stdio.h>
#include <string.h> 
#define SIZE 70

int isLegitSequence(char sequence[]);
void getComplementaryStrand(char complementary[],char sequence[]);
int findSubSequence(char sequence[],char subsequence[]);
int findSubComplementary(char sequence[],char subcomplementary[]);
void cutSequence(char sequence[],char tocut[]);
void checkDNAList(char data[][SIZE],int rows,char sequence[]);


void main(){
    char dnaSequence[SIZE];
    char compDnaSequence[SIZE];

    printf("Enter a DNA Strand\n");
    gets(dnaSequence);
    printf("%d\n",isLegitSequence(dnaSequence));
    getComplementaryStrand(compDnaSequence,dnaSequence);
    puts(compDnaSequence);

}

int isLegitSequence(char sequence[]){
    int i=0;
    while (sequence[i]){
        if(sequence[i]=='A'||sequence[i]=='C'||sequence[i]=='G'||sequence[i]=='T');
        else return 0;
        i++;
    }
    return 1;
}

void getComplementaryStrand(char complementary[SIZE],char sequence[SIZE]){
    int j=strlen(sequence)-1,i;
    for(i=0;sequence[i];i++,j--){
        if(sequence[i]=='A') sequence[j]='T';
        else if(sequence[i]=='C') sequence[j]='G';
        else if(sequence[i]=='G') sequence[j]='C';
        else sequence[j]='A';
    }
    complementary[strlen(sequence)]='\0';
}

However, this is what I get when I run the program: 但是,这是我运行程序时得到的:

Enter a DNA Strand
CGCTC
1
╠╠╠╠╠
Press any key to continue . . .

This is my first time using functions, so I'm not sure what I did wrong here. 这是我第一次使用函数,所以我不确定在这里做错了什么。 Would appreciate help, but in the scope of my understanding, namely very very basic. 希望能有帮助,但在我的理解范围内,即非常非常基础。

You need to add a prototype of the function getComplementaryStrand at the top of of the source file where the function is called. 您需要在调用该函数的源文件顶部添加函数getComplementaryStrand的原型。

Add this line at the top of the source file: 将此行添加到源文件的顶部:

void getComplementaryStrand(char complementary[SIZE],char sequence[SIZE]);

EDIT : the question has changed in the meantime... Before it was a compilation error. 编辑 :在此期间问题已更改...在它是编译错误之前。 OP please ask a new question instead of editing your original question with a new question. OP,请问一个新问题,而不是用一个新问题编辑您的原始问题。

In getComplementaryStrand() you never fill anything in complementary string except end character. getComplementaryStrand()中,除了结尾字符外,您永远不会在补充字符串中填充任何内容。 So you get garbage. 这样你就会得到垃圾。

Take a careful look at your for loop within your getComplementaryStrand() function. 请仔细查看getComplementaryStrand()函数中的for循环。 Are you assigning values to the correct string? 您是否正在为正确的字符串分配值? I think not. 我想不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM