[英]Using strstr to find all instances of substring results in weird string formatting
[英]How to express “if substring present in string do something” when using strstr?
我正在尝试编写一个非常简单的程序,该程序需要一个字符串(氨基酸或核酸),并检查一个子字符串出现多少次并输出结果。
但是,我不知道如何在while if循环中声明加1以仅在存在匹配项时计数,并在到达行尾时制动。 由于strstr的输出是指向匹配位置的指针,因此我无法弄清楚如何将字符串等价表示为if()的逻辑条件。
#include<stdio.h>
#include<string.h>
int main(){
int j,count;
char *i,dna_seq[50], desired_seq[3]="AT";
while(1){
printf("\nPlease insert protein sequence:\n");
gets(dna_seq);
printf("\nPlease insert searched sequence:\n");
gets(desired_seq);
while(1){
strstr(dna_seq,desired_seq);
if(//**answer**//);
count++;
dna_seq[j]++;
if(i=='\0') break;
}
printf("\n\nOccurance of %s: %d times\n",desired_seq,count);
}
}
您还会为调试写什么(因为我正在尝试练习)?
预先感谢您的帮助,如果问题没有明确提出,对不起。
strstr
返回一个指向您要查找的字符串的出现的指针,如果不存在则返回NULL
。 因此,您应该循环执行直到获得NULL
,并且每次获得有效的指针时,都要递增计数器,然后从其后立即开始寻找:
char* loc = NULL;
while (loc = strstr(prot_seq,desired_seq)) {
count++;
loc++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.