反转 DNA 字符串序列并找到其互补序列

发布于 2025-01-11 08:44:15 字数 640 浏览 0 评论 0原文

我很难为此编写 C 程序。我想使用 strrev 函数来反转字符串。然而,在求补时,它总是说“指针与整数之间的比较”。

请纠正我的 if else 语句。我非常需要它。谢谢。

#include<stdio.h> // define the header file
#include<string.h>
void main() {  // define the main function
    char str[100];
    char comp[100];
    printf("\nHi! \n Please input a string: ");
    scanf("%s",&str);
    printf("Reversed String: %s", strrev(str));
    
    if(str == 'a')
    comp=='t';
    else if(str == 't')
    comp=='a';
    else if(str == 'g')
    comp=='c';
    else if(str == 'c')
    comp=='g';

    printf("\nComplementary: %s",comp);
}

I am having a hard time doing C program for this. I have come up to use strrev function for reversing a string. However, in finding the complementary, it always says COMPARISON BETWEEN POINTER AND INTEGER.

Please correct my if else statement. I badly need it. Thank you.

#include<stdio.h> // define the header file
#include<string.h>
void main() {  // define the main function
    char str[100];
    char comp[100];
    printf("\nHi! \n Please input a string: ");
    scanf("%s",&str);
    printf("Reversed String: %s", strrev(str));
    
    if(str == 'a')
    comp=='t';
    else if(str == 't')
    comp=='a';
    else if(str == 'g')
    comp=='c';
    else if(str == 'c')
    comp=='g';

    printf("\nComplementary: %s",comp);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2025-01-18 08:44:15

您需要使用赋值运算符 = 进行赋值(而不是 ==,这是为了比较)。您需要循环遍历字符串的元素 - 例如,for (size_t i = 0; str[i] != '\0'; i++) 并使用 引用字符str[i] 在循环体中设置互补字符串。

我想,您可能还需要担心大写字母和小写字母 — 我发现 ACGT 在 DNA 基础分子中的使用频率比 acgt 更频繁。下面的代码识别无效字母,但仍然继续。

请注意,虽然 Microsoft 提供了 strrev() 函数,但它并未由 C 标准或 POSIX 标准化。

#include <stdio.h>
#include <string.h>

int main(void)
{
    char str[100];
    char comp[100];

    printf("\nHi!\nPlease input a DNA string (lower-case acgt): ");
    scanf("%99s", str);
    printf("Reversed String: %s\n", strrev(str));
    
    size_t i;
    for (i = 0; str[i] != '\0'; i++)
    {
        if (str[i] == 'a')
            comp[i] = 't';
        else if (str[i] == 't')
            comp[i] = 'a';
        else if (str[i] == 'g')
            comp[i] = 'c';
        else if (str[i] == 'c')
            comp[i] = 'g';
        else
        {
            fprintf(stderr, "Invalid DNA character '%c' at position %zu\n", str[i], i);
            comp[i] = str[i];
        }
    }
    comp[i] = '\0';  // Make sure the complementary string is null terminated

    printf("\nComplementary: %s\n", comp);
    return 0;
}

You need to use assignment operator = to assign (not ==, which is for comparison). You need to loop over the elements of the string — for example, for (size_t i = 0; str[i] != '\0'; i++) and refer to the characters using str[i] in the body of the loop setting up the complementary string.

You might need to worry about upper-case vs lower-case letters too, I think — I've seen ACGT used more often than acgt for the DNA base molecules. The code below identifies invalid letters, but continues anyway.

Note that while Microsoft provides the strrev() function, it is not standardized either by the C standard or by POSIX.

#include <stdio.h>
#include <string.h>

int main(void)
{
    char str[100];
    char comp[100];

    printf("\nHi!\nPlease input a DNA string (lower-case acgt): ");
    scanf("%99s", str);
    printf("Reversed String: %s\n", strrev(str));
    
    size_t i;
    for (i = 0; str[i] != '\0'; i++)
    {
        if (str[i] == 'a')
            comp[i] = 't';
        else if (str[i] == 't')
            comp[i] = 'a';
        else if (str[i] == 'g')
            comp[i] = 'c';
        else if (str[i] == 'c')
            comp[i] = 'g';
        else
        {
            fprintf(stderr, "Invalid DNA character '%c' at position %zu\n", str[i], i);
            comp[i] = str[i];
        }
    }
    comp[i] = '\0';  // Make sure the complementary string is null terminated

    printf("\nComplementary: %s\n", comp);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文