This article for everyone to share the C + + calculation of the number of occurrences of each character code, for your reference, the specific content as follows
#include <iostream>//#include <cstdlib> #include <ctime> using namespace std;
const int number_of_letters = 26;
const int number_of_random_letters = 100;
void Createarray (char []);
void Displayarray (const char []);
void Countletters (const char [], int []);
void displaycounts (const int []);
int main () {char chars[number_of_random_letters];
Createarray (chars);
cout << "The lowercase letters are:" << Endl;
Displayarray (chars);
int counts[number_of_letters];
Countletters (chars, counts);
cout << "\nthe occurrences of each letter are:" << Endl;
Displaycounts (counts);
return 0;
} void Createarray (char chars[]) {srand ((unsigned int) time (0)); for (int i = 0; i < number_of_random_letters i++) {Chars[i] = static_cast<char> (' A ' + rand ()% (' Z '-' a ')
+ 1)); } void Displayarray (const char chars[]) {for (int i = 0; i < number_of_random_letters; i++) {if ((i + 1) % = = 0) cout <<
Chars[i] << "" << Endl;
else cout << Chars[i] << ""; } void Countletters (const char chars[], int counts[]) {for (int i = 0; i < number_of_letters; i++) Counts[i]
= 0; for (int i = 0; i < number_of_random_letters i++) Counts[chars[i]-' a ']++; Classic} void displaycounts (const int counts[]) {for (int i = 0; i < number_of_letters; i++) {if ((i + 1)% 1
0 = 0) cout << counts[i] << "" << static_cast<char> (i + ' a ') << Endl;
else cout << Counts[i] << "" << static_cast<char> (i + ' a ') << "";
} cout << Endl;
}
The above is the entire content of this article, I hope to help you learn.