Daniel Radcliffe
Daniel Radcliffe

Reputation: 29

Finding number of occurences of all characters in three strings in C++

I don't understand why this program is not working.

I need to find the number of occurrences of all characters in three strings by any method.

I used count method but if you guys can help me out with find function so it would be better.

#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    string line[3];
    int count[3];
    cout << "Enter three lines of text...\n\n";
    cin >> line[0];
    cin >> line[1];
    cin >> line[2];
    int i;
    for(char j='a'; j<=26; j++) {
        for(i=0; i<3; i++)
            count[i] = std::count(line[i].begin(), line[i].end(), j);
        cout << "\n" << j << "\t" << ":" << "\t" << count[i];
    }

    system ("pause");
    return 0;
}

Upvotes: 1

Views: 963

Answers (5)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24153

int i;
for(char j='a'; j<=26; j++) {
    for(i=0; i<3; i++)
        count[i] = std::count(line[i].begin(), line[i].end(), j);
    cout << "\n" << j << "\t" << ":" << "\t" << count[i];
}

Your cout line isn't in the inner for loop.

After the inner loop finishes, i is 3. Then cout is called with count[i] which goes out of bounds on the array. Undefined behaviour, you're lucky if it crashes.

Your problem is the i. The place it's declared means it still exists and the cout line can reference it. If you move the declaration to the loop initialiser you'll find the next error:

for(int i=0; i<3; i++)
    count[i] = std::count(line[i].begin(), line[i].end(), j);
cout << "\n" << j << "\t" << ":" << "\t" << count[i];

The last line won't compile as i goes out of scope at the end of the loop. I assume you were mistaken in thinking cout was part of the loop, partly due to the poor formatting of the code, and secondly because i was declared at a wider scope.

To correct this, create a block for the for loop by using { and }. This will keep the i in scope for all statements within the block, and repeat the statements for each iteration of the loop.

for(int i=0; i<3; i++) {
    count[i] = std::count(line[i].begin(), line[i].end(), j);
    cout << "\n" << j << "\t" << ":" << "\t" << count[i];
}

Upvotes: 0

ThePosey
ThePosey

Reputation: 2734

http://www.asciitable.com/

Lower case 'a' is 96. Which is less than 26 which is why your loop doesn't execute. Try:

for (char j = 'a'; j <= 'z'; j++)

This will only count the lower case characters. If you wanted the occurrence of lower and upper case you could do this:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    string line[3];

    cout << "Enter three lines of text...\n\n";
    cin >> line[0];
    cin >> line[1];
    cin >> line[2];

    for(char j='a';j<='z';j++)
    {
        int upper_sum = 0;
        int lower_sum = 0;

        for(int i=0;i<3;i++)
        {
            lower_sum += std::count(line[i].begin(),line[i].end(),j);
            upper_sum += std::count(line[i].begin(),line[i].end(),j - 32); //32 = 'a' - 'A'
        }

        cout<<"\n"<<j<<"\t"<<":"<<"\t"<<lower_sum;
        cout<<"\n"<<(char)(j - 32)<<"\t"<<":"<<"\t"<<upper_sum;
    }
    return 0;
}

Upvotes: 1

Andy Prowl
Andy Prowl

Reputation: 126512

I would go for something like this:

#include <map>
#include <string>
#include <iostream>

int main()
{
    // Given 3 strings...
    std::string s1 = "Hello";
    std::string s2 = "Cruel";
    std::string s3 = "World";

    //===============================================
    // THESE 2 LINES ARE ALL YOU NEED FOR COUNTING
    std::map<char, int> countMap;
    for (char c : (s1 + s2 + s3)) { countMap[c]++; }
    //===============================================

    // Print the output. This is if you do not care about
    // characters that do not appear at all.
    for (auto const& e : countMap)
    {
        std::cout << e.first << ": " << e.second << std::endl;
    }

    // Print the output. This is if you DO care about
    // characters that do not appear at all.
    for (char c = ' '; c <= '~'; c++)
    {
        std::cout << c << ": " << countMap[c] << std::endl;
    }
}

Upvotes: 3

Tony The Lion
Tony The Lion

Reputation: 63250

Here's a correct version of your program:

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
string line[3];
int count[3];
/*cout<<"Enter three lines of text...\n\n";
cin>>line[0];
cin>>line[1];
cin>>line[2];*/

      line[0] = "aaaaabbbbbbbbbbb";
      line[1] = "ccccccddddddddddd";
      line[2] = "kkkkkkkkkkkkk";

     int i;
     for(char j='a';j<= 'z';j++) // You can loop through the alphabet, but you need to go 'a' to 'z' and not 26
     { 

             for(i=0;i<3;i++) 
             { // You were missing these braces,  you need them if your loop contains multiple lines
               count[i]= std::count(line[i].begin(),line[i].end(),j);
               cout<<"\n"<<j<<"\t"<<":"<<"\t"<<count[i];
             }
    }

    system ("pause");
    return 0;

}

And here it is in action.

Upvotes: 0

Arne Mertz
Arne Mertz

Reputation: 24626

26 is no letter, and (char)26 is normally less than 'a' - so your loop will not execute. Try char j='a';j<='z';j++

Upvotes: 3

Related Questions