How do I count occurrences of upper and lower case letters as the same?

Write your question here.
I'm writing a code that counts the number of occurrences of each letter and then output the number of times for each. However I want the code to treat upper and lower case letters as the same for example: aa AA would equal 4 occurrences of a. Also I know that my code is a lot longer than necessary, but right now I just need to count lower and upper case as the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <algorithm>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    string myString;
    
    std::cout << "Please enter your string: " << endl;
    getline (cin,myString);
      
    std::cout << "Output: "<< endl
              
              << "a"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'a') << endl 
              << "b" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'b') << endl
              << "c"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'c') << endl 
              << "d" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'd') << endl
              << "e"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'e') << endl 
              << "f" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'f') << endl
              << "g"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'g') << endl 
              << "h" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'h') << endl
              << "i"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'i') << endl 
              << "j" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'j') << endl
              << "k"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'k') << endl 
              << "l" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'l') << endl
              << "m"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'm') << endl 
              << "n" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'n') << endl
              << "o"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'o') << endl 
              << "p" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'p') << endl
              << "q"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'q') << endl 
              << "r" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'r') << endl
              << "s"
              << setw(5)
              << std::count(myString.begin(), myString.end(), 's') << endl 
              << "t" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 't') << endl
              << "u" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'u') << endl
              << "v" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'v') << endl
              << "w" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'w') << endl
              << "x" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'x') << endl
              << "y" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'y') << endl
              << "z" 
              << setw(5)
              << std::count(myString.begin(), myString.end(), 'z') << endl;


    return 0;
}

One way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct caseless_compare {
    char ch;
    caseless_compare(char ch) :ch(std::tolower(ch)) {}

    bool operator()(char _ch) const {
        return ch == std::tolower(_ch);
    }

};

int main()
{
    string myString;

    std::cout << "Please enter your string: " << endl;
    getline(cin, myString);



    std::cout << "Output:\n";
    for (char ch = 'a'; ch <= 'z'; ++ch)
    {
        std::cout << ch << std::setw(5);
        std::cout << std::count_if(myString.begin(), myString.end(), caseless_compare(ch)) << '\n';
    }


    return 0;
}
I just need to count lower and upper case as the same.


Why do you count then only the lower case characters?

Much easier if you convert the input either to lower or upper case. Also counting can easily be done in an array. A has the ASCII code of 65 so you can easily convert it to an array index.
Here is a demo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string myString;

  std::cout << "Please enter your string: ";
  getline (cin,myString);
  for_each(myString.begin(), myString.end(), [](char& ch) {ch = (char)toupper(ch);});

  int count[26] = {0};

  for_each(myString.begin(), myString.end(), [&](const char& ch) {count[ch - 65]++;});

  for (int i = 0; i < 26; i++)
  {
    cout << "count[" << (char) (i + 65) << "] = " << count[i] << "\n";
  }

  system("pause");
  return 0;
}
Topic archived. No new replies allowed.