是什么导致我的C字符串函数在生成互补DNA序列时在0以外的位置失败?

aelbi1ox  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(124)

我想写一个函数,它接受一个DNA序列,并输出互补序列。例如,将序列ATGC更改为TACG。我写的函数就是为了这个目的。但是,我希望函数只在我定义的位置生成互补序列。但是如果我为位置传递0以外的值,我编写的函数就不起作用。
我不知道是什么问题,因为我没有得到一个编译器错误或分割故障。

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

void compliment(string seq, string comp_seq, int pos, int lenght);

int main(void)
{
    string DNA_seq = get_string("Input: ");

    char compliment_DNA_seq[strlen(DNA_seq) + 1];

    compliment(DNA_seq, compliment_DNA_seq, 0, 22);

    printf("\n%s\n%s\n\n", DNA_seq, compliment_DNA_seq);
}

void compliment(string seq, string comp_seq, int pos, int lenght)
{
    int j = lenght;
    for (int i = pos; j > 0; i++, j--)
    {
        if (seq[i] == 65)
        {
            comp_seq[i] = 84;
        }
        else if (seq[i] == 67)
        {
            comp_seq[i] = 71;
        }
        else if (seq[i] == 71)
        {
            comp_seq[i] = 67;
        }
        else
        {
            comp_seq[i] = 65;
        }
    }
}
zbdgwd5y

zbdgwd5y1#

解决此问题的一种方法是使用strcpy()将整个DNA_seq复制到complimentary_DNA_seq中。在使用strcpy()时必须小心:在还考虑终止空字符之后,目的地缓冲区应该大于或等于源缓冲区。注意:通过执行string complimentary_DNA_seq = DNA_seqDNA_seq分配给complimentary_DNA_seq会导致浅拷贝。对complimentary_DNA_seq的任何更改也会更改DNA_seq
此外,正如Shawn在评论中所说,直接使用字符文字操作字符串的字符比使用ASCII值更可取。

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

int compliment(string seq, string comp_seq, int pos, int length);

int main(void)
{
    string DNA_seq = get_string("Input: ");

    char compliment_DNA_seq[strlen(DNA_seq) + 1];
    // Copy the original sequence into the complimentary sequence
    // Note: string compliment_DNA_seq = DNA_seq; leads to a shallow copy.
    // We want a deep copy.
    strcpy(compliment_DNA_seq, DNA_seq);

    int error_flag = compliment(DNA_seq, compliment_DNA_seq, 2, 3);

    if (error_flag == -1) {
      return -1;
    }

    printf("\n%s\n%s\n\n", DNA_seq, compliment_DNA_seq);

    return 0;
}

// TODO: the function could be updated to receive only comp_seq
// Since, it already is a copy of seq.
int compliment(string seq, string comp_seq, int pos, int length)
{
    // Do some error handling
    if (pos >= strlen(seq)) {
        printf("Enter a position within the sequence.\n");
        return -1;
    }
    else if (pos + length > strlen(seq)) {
        printf("Trying to copy beyond the limit of the input sequence.\n");
        return -1;
    }

    // compliment the characters in [pos, pos + length)
    int j = length;
    for (int i = pos; j > 0; i++, j--)
    {
        // manipulating the characters directly with
        // character literals.
        if (seq[i] == 'A')
        {
            comp_seq[i] = 'T';
        }
        else if (seq[i] == 'C')
        {
            comp_seq[i] = 'G';
        }
        else if (seq[i] == 'G')
        {
            comp_seq[i] = 'C';
        }
        else
        {
            comp_seq[i] = 'A';
        }
    }
    return 0;
}

相关问题