C + + calculates the number of occurrences per character _c language

Source: Internet
Author: User

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 &LT;&LT
    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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.