欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++計(jì)算每個字符出現(xiàn)的次數(shù)

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

本文實(shí)例為大家分享了C++計(jì)算每個字符出現(xiàn)的次數(shù)的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下

#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']++; //經(jīng)典
}
 
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;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

上一篇:簡單講解C語言中宏的定義與使用

欄    目:C語言

下一篇:C語言中交換int型變量的值及轉(zhuǎn)換為字符數(shù)組的方法

本文標(biāo)題:C++計(jì)算每個字符出現(xiàn)的次數(shù)

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2346.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有