Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Find all indices in a char array A at which a pattern P occurs. This program has

ID: 3806831 • Letter: F

Question

Find all indices in a char array A at which a pattern P occurs. This program has the user input a longish character array A, and then a shorter character array P(P contains the pattern), and constructs and displays an array -which we will call I - containing a indices at which (possibly overlapping) occurrences of P start in A, in the order in which these occur. Thus if A is a b a a b a a a b a a a a b b and P is a b, then l will contain 0, 3, 7, and 12. And if instead P is a a, then the contents of I would be 25 69 10 11-note carefully how the overlaps factor in here. Another example: if A is a b a b a b a b c c c d and P is a b a, then I contains 0 2 4 When the program prints the array I, it prints only the part of I which has been used -see the text's discussion of partially filled arrays. By setting the global const bool nonOverlappingOnly to true, the program reports only non-overlapping occurrences of P In this case, the output for the first example above is 2 5 9 11, and the output for the second example is 0 4

Explanation / Answer

#include <iostream>

using namespace std;
class charc
{
public:
    char A[20]={'a','b','a','a','b','a','a','a','b','a','a','a','a','b','b',''};
    char P[5]={'a','a',''};
    int I[10];
    int ilen,inc=0;
    bool nonOverlapping=true;
    void index()//With Overlapping
    {
        int iloop;
        for(iloop=0;A[iloop]!='';iloop++);
        ilen=iloop;
        for(iloop=0;iloop<ilen;iloop++)
        {
            if(A[iloop]==P[0]&&A[iloop+1]==P[1])
            {
                I[inc]=iloop;   //store the index of matched pattern
                inc++;
            }
        }
        cout<<"With Overlapping Index: "<<endl;
        for(iloop=0;iloop<inc;iloop++)
        {
            {
                cout<<I[iloop]<<" ";//print the array
            }
        }
    }
        void overlap()//Without Overlapping
        {
        inc=0;
        int iloop;
        for(iloop=0;iloop<ilen;iloop++)
        {
            if(A[iloop]==P[0]&&A[iloop+1]==P[1])
            {
                I[inc]=iloop;   //store The index without overlapping
                inc++;
                iloop=iloop+1; /*so that if it overlap don't occur increment the pointer by 1 for two char pattern similarly increment 2 for three char pattern */
            }
        }
        cout<<" Without Overlapping Index: "<<endl;
        for(iloop=0;iloop<inc;iloop++)
        {
            {
                cout<<I[iloop]<<" ";   //print the array
            }
        }

    }

};
int main()
{
    charc c;
    c.index();
    c.overlap();

    return 0;
}


/**********OUTPUT******
With Overlapping Index:

2 5 6 9 10 11
Without Overlapping Index:

2 5 9 11
****************************/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote