#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) % 20 == 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']++; //经典
}
void displayCounts(const int counts[])
{
for (int i = 0; i < NUMBER_OF_LETTERS; i++)
{
if ((i + 1) % 10 == 0)
cout << counts[i] << " " << static_cast<char>(i + 'a') << endl;
else
cout << counts[i] << " " << static_cast<char>(i + 'a') << " ";
}
cout << endl;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有