Hello,

I have a problem about counting consequtive characters, here my code

char query[] = "tttthe oneZZZZZ    trully loveeee";


void cntfreq(char A[])
{
    int j;
    int k=0;
    j = 1;
    while (A[j] != '\0')
    {
        if (A[j-1] == A[j])
        {
            ++k;
            cout<<k<<"\t"<<A[j]<<endl;
        }
        else
            k = 0;
        ++j;
    }
}

Suppose that also my char is the following as an input:

char query[] = "tttthe oneZZZZZ    trully loveeee";

I success in counting the consequtive characters, but it counts minus 1, for example there are 4 consecutive char 't' but it says 3 't' or 5 'Z' character, but it outputs 4 Z are here. Actually, I found the problem in which occurs at checking last consecutive characters, in other words, when it checks t and h or last character e and '\0' character at the end of file. I found the problem in my mind, but how I translate in C++ code ? It is my part of my homework, honestly but it is not C++ homework; it is Mips homework, I'll try to translate this "working/proven" C++ code into Mips language, I hope.

Thanks.

You need to start k at 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.