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

Complete the body of this function. Use a queue of characters to store the input

ID: 674789 • Letter: C

Question

Complete the body of this function. Use a queue of characters to store the input line as it is being read.

int counter( )

    // Precondition: There is a line of input waiting to be read from cin.

    // Postcondition: A line of input has been read from cin, up to but not

    // including the newline character. The return value of the function

    // is the number of times that the LAST character of the line appeared

    // somewhere in this line.

    // EXAMPLE Input: ABBXDXXZX

    // The value returned by counter would be 4 for this input since there

    // are 4 X's in the input line.

    {

        int answer = 0;

        queue q;

Explanation / Answer

The solution is as follows:

int counter()

{

    char a[100];

    int i=0;

    int answer=0;

    Queue q;

    cin.getline(a,98,' ');

    for(i=0;i<strlen(a);i++)

    {

        q.enqueue(a[i]);

    }

   i--;

    while(!q.isEmpty())

    {

        if(a[i]==q.dequeue())

        {

            answer++;

        }

    }

    return answer;

}

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