简体   繁体   中英

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.

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.

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.

In getComplementaryStrand() you never fill anything in complementary string except end character. So you get garbage.

Take a careful look at your for loop within your getComplementaryStrand() function. Are you assigning values to the correct string? I think not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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