I am trying to write a very simple program, which takes a string of characters(amino acids, or nucleic acids) and checks how many times a sub-string appears and outputs the result.
However, I don't know how to state in the while if loop to add 1 to count only if there has been a match and to brake if it reaches end of the line. Since the output of strstr is a pointer towards the position of the match, I can't figure out how to express string equivalence as a logical condition for the 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);
}
}
Also what would you write for debugging (since I am trying to practice)?
Thank you in advance for the help, sorry if the question was not clearly posed.
strstr
returns a pointer to the occurance of the string you're looking for, or NULL
if it's not present. So you should loop until you get NULL
, and every time you get a valid pointer, increment the counter, and start looking from right after it:
char* loc = NULL;
while (loc = strstr(prot_seq,desired_seq)) {
count++;
loc++;
}
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.