I found a quite interesting problem on the Internet, and of course, out of curiosity, I tried to solve it. Too bad nothing came to an end :(.
The task was to count the alphabet's upper letters appearing in a given text and to print on the screen all the alphabet's letters and the rate of appearing (how many times was that letter in the given text).
What I created didn't have any result, unfortunately.
I tried, in a "for" loop, to count the appearing of every letter, using strchr (alphabet, text's current letter); considering my text in an array. No idea what my program did, because it shut my C++ program, so I lost it, but it's for sure that it didn't work.
If you have any other ideas on how to solve this, I'd dying out of curiosity.
Thanks && Cheers !

Start of by giving us an example of your code. So we can see where you are going wrong. Also pointing us to the bit you think is the problem would be good.

Sounds an awful lot like a homework assignment, C++ would not delete your source file if it crashed.

I found a quite interesting problem on the Internet, and of course, out of curiosity, I tried to solve it. Too bad nothing came to an end :(.
The task was to count the alphabet's upper letters appearing in a given text and to print on the screen all the alphabet's letters and the rate of appearing (how many times was that letter in the given text).
What I created didn't have any result, unfortunately.
I tried, in a "for" loop, to count the appearing of every letter, using strchr (alphabet, text's current letter); considering my text in an array. No idea what my program did, because it shut my C++ program, so I lost it, but it's for sure that it didn't work.
If you have any other ideas on how to solve this, I'd dying out of curiosity.
Thanks && Cheers !

Hi u might consider using the

islower function

which checks if character is lowercase letter within your loop.

P.s. u need <cctype> header for this to work

Hi u might consider using the

which checks if character is lowercase letter within your loop.

P.s. u need <cctype> header for this to work

That or a huge IF, right ?
I solved it with an IF.

And yes, it deleted the file when it crashed, because it wasn't yet saved.

And no, it's not a homework, because I'm over 30. I just found this on a website, like many other problems.

Cheers, mate!

No need in strchr, islower etc for latin letters only.

void CountUppers(std::istream& is)
{
    if (!is)
        return;
    int cnt['Z'-'A'+1] = {0};
    char c;
    while (is.get(c)) {
        if (c >= 'A' && c <= 'Z')
            ++cnt[c-'A'];
    }
    for (c = 'A'; c <= 'Z'; ++c)
        std::cout << c << '\t' << cnt[c-'A'] << '\n';
    std::cout.flush();
}

int main(int argc, char* argv[])
{
    std::string test("XYZ111");
    std::istringstream s(test);
    CountUppers(s);
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.