Newbie needs help

I try to use "rand" to create 100 string, I'm happy I succeed, but next step i wanna know how to count each number of letters and the frequency it shows.

Here is the code:

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iostream>

// using namespace std;


int main()
{
const char *c = "abcdefghijklmnopqrstuvwxyz";

srand(time(NULL));

for(int i = 0; i < 100; ++i)
{
std::cout << c[rand() % strlen(c)] << " ";

}
system ("pause");
return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>

#define NO_OF_CHARS 26

int main()
{
  const char *c = "abcdefghijklmnopqrstuvwxyz";
  int char_count[NO_OF_CHARS] = {0};
    
  srand(time(NULL));
  // Generate 100 random character noting down their repeat count.
  for(int i = 0; i < 100; ++i)
  {
    int j = rand() % strlen(c);
    std::cout << std::endl << c[j];
    char_count[j]++;
  }
  // Print the repeat count of each character.
  for(int i = 0; i < NO_OF_CHARS; ++i)  
    std::cout << std::endl << c[i] << ": " << char_count[i];
  
  return 0;
}

Topic archived. No new replies allowed.