In input.txt, I have a sentence, for example:
HELLO COUNT HOW MANY CHARACTERS ARE HERE AND WHICH CHARACTERS
I have to read input.txt and count how many characters of each letter are in that sentence. And then, I have to sort those characters in a descending order.
I'll give you an example, there are:
H:7, E:6, L:2, O:3, C:5, W:2, A:7, Y:1, R:6, S:2, I:1, M:1, N:1
A letter indicates which letter it is, and a number indicates how many times that letter appears in the sentence. And when I sort them, it should like this:
H:7, A:7, E:6, R:6, C:5, O:3, L:2, W:2, S:2, Y:1, I:1, M:1, N:1
It doesn't matter which letter is first if they appear the same amount of times.
The problem is that I don't know how to sort them, and how to make that each letter gets printed in a sorted order. I'm new to C++ so I don't know much.
The only idea I came up with is to put all of those letters into a struct and then sort it. I've thought about putting them into an array, but I'm not sure how to do that, or if it's possible. So I decided to try it with a struct, but didn't make it far.
Here's my code:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct letters{
    int A=0, C=0, E=0, H=0, I=0, L=0, M=0, N=0, O=0, R=0, S=0, W=0, Y=0;
    int n=13;
};
int main() {
  string str;
  int A=0, C=0, E=0, H=0, I=0, L=0, M=0, N=0, O=0, R=0, S=0, W=0, Y=0;
  int n=13; // How many letters there are in total
  ifstream read("input.txt");
  while (getline(read,str)) {
    for(char &ch : str) {
      // Here I read a letter and if it matches one of those "if statements" it counts it
      if(ch == 'A'){
        A++;
      }
      if(ch == 'C'){
        C++;
      }
      if(ch == 'E'){
        E++;
      }
      if(ch == 'H'){
        H++;
      }
      if(ch == 'I'){
        I++;
      }
      if(ch == 'L'){
        L++;
      }
      if(ch == 'M'){
        M++;
      }
      if(ch == 'N'){
        N++;
      }
      if(ch == 'O'){
        O++;
      }
      if(ch == 'R'){
        R++;
      }
      if(ch == 'S'){
        S++;
      }
      if(ch == 'W'){
        W++;
      }
      if(ch == 'Y'){
        Y++;
      }
    }
  }
  letters a[n];
  sort(a, a+n); // Trying to sort it and then print everything out like I did below. But I don't know how
  // Here I just check if every letter is counted correctly
  cout << "A: " << A << endl;
  cout << "C: " << C << endl;
  cout << "E: " << E << endl;
  cout << "H: " << H << endl;
  cout << "I: " << I << endl;
  cout << "L: " << L << endl;
  cout << "M: " << M << endl;
  cout << "N: " << N << endl;
  cout << "O: " << O << endl;
  cout << "R: " << R << endl;
  cout << "S: " << S << endl;
  cout << "W: " << W << endl;
  cout << "Y: " << Y << endl;
  read.close();
  return 0;
}
 
     
     
    