反转 DNA 字符串序列并找到其互补序列
我很难为此编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用赋值运算符 = 进行赋值(而不是 ==,这是为了比较)。您需要循环遍历字符串的元素 - 例如,
for (size_t i = 0; str[i] != '\0'; i++)
并使用引用字符str[i]
在循环体中设置互补字符串。我想,您可能还需要担心大写字母和小写字母 — 我发现 ACGT 在 DNA 基础分子中的使用频率比 acgt 更频繁。下面的代码识别无效字母,但仍然继续。
请注意,虽然 Microsoft 提供了
strrev()
函数,但它并未由 C 标准或 POSIX 标准化。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 usingstr[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.