1

我正在尝试使用 strstr 函数来计算字符串“TT”出现在 DNA 序列 ATGCTAGTATTTGGATAGATAGATAGATAGATAGATAGATAAAAAAATTTTTTTT 的次数,而不计算任何“T”的两次。它应该带有 5 个 'TT' 实例,但我的函数给了我 9 个,如果你重叠了 'TT',你会得到。我该如何解决这个问题,以便只计算“TT”的每个单独实例而没有 T 被计算两次?这是我的程序:

/***************************************************************************************/
#include <iostream>    
#include <cstring>      
#include <iomanip>

using namespace std;

    //FUNCTION PROTOTYPES
     int overlap(char *ptr1, char *ptr2);

int main()
{

    //Declare and initialize objects
   int count(0); // For DNA sequence

        //DNA SEQUENCE
    char DNA_sequence[] = "ATGCTAGTATTTGGATAGATAGATAGATAGATAGATAGATAAAAAAATTTTTTTT";
    char thymine_group[] = "TT";
    char *ptr1(DNA_sequence), *ptr2(thymine_group);

//Send QUOTE to function
count = overlap(ptr1, ptr2);

   //Print number of occurences.
    cout << "'TT' appears in DNA sequence " << count << " times" << endl;
    return 0;
}

//FUNCTION 1 USING CHAR ARRAYS AND POINTERS

int overlap(char *ptr1, char *ptr2)
{
    int count(0);
    //Count number of occurences of strg2 in strg1.
    //While function strstr does not return NULL
    //increment count and move ptr1 to next section
    //of strg1.
    while ((ptr1=strstr(ptr1,ptr2)) != NULL)
    {
        count++;
        ptr1++;
    }
    return count;
}

/**************************************************************************************************/
4

3 回答 3

7

只需将ptr1++;您的循环更改为ptr1 += strlen(ptr2);

于 2011-07-25T01:18:18.427 回答
0

尝试

int overlap(char *ptr1, char *ptr2)
{
    int count(0);
    //Count number of occurences of strg2 in strg1.
    //While function strstr does not return NULL
    //increment count and move ptr1 to next section
    //of strg1.
    while ((ptr1=strstr(ptr1,ptr2)) != NULL)
    {
        count++;
        ptr1 += strlen (ptr2);
    }
    return count;
}
于 2011-07-25T01:19:15.027 回答
0
int count(char *haystack, char* needle)
{       int c = 0;
        for(;*haystack;haystack++){
            if(strcmp(haystack, needle)==0){
                 c++;
                 haystack+=strlen(needle)-1;
            }
        }
        return c;
}
于 2011-07-25T01:22:56.133 回答