Hi folks,

I've been trying to solve this but I keep hitting a brick wall. Here's what I am trying to do: I'm reading a file with text in it, I am storing every letter of this file as an element of this array (this part is already done) and I've also terminated the array with \0. Now, I want to be able to count how many repetitions are for each letter.

What I was thinking of doing is:

- Create a new array to hold the letters (E.g letters [26] (english alphabet only)
- The array would look like {45,23,434,35,....,90}
- So if letter[0] = 45 that would mean there are 45 a's or letter[1] = 99 there are 99 b's

What I'm having problems with is, how do I iterate through textStr and add this type of functionality?

Dani AI

Generated

Nice work on the breakthrough, — and good question from about seeing exact output. "Normalize" usually means turning raw counts into comparable values: percentages, a fixed-width ASCII bar (histogram), or sorting by frequency. Below are small, copy-pasteable helpers (C and C++) that compute a total, guard against zero, print aligned counts with two-decimal percentages, and draw a simple bar scaled to a chosen width. They make it easy to verify what the program is doing when compiled.

#include <stdio.h>

void print_normalized(const unsigned long counts[26]) {
    unsigned long total = 0;
    for (int i = 0; i < 26; ++i) total += counts[i];
    if (total == 0) { puts("No letters found."); return; }
    const int bar_width = 50;
    const char *bars = "##########""##########""##########""##########""##########";
    for (int i = 0; i < 26; ++i) {
        double pct = (double)counts[i] * 100.0 / (double)total;
        int bar = (int)((pct * bar_width / 100.0) + 0.5);
        printf("%c: %6lu  %6.2f%%  |%.*s\n",
               'a' + i, counts[i], pct, bar, bars);
    }
}
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

void print_normalized(const std::vector<size_t>& counts) {
    size_t total = 0;
    for (auto v : counts) total += v;
    if (total == 0) { std::cout << "No letters found.\n"; return; }
    const int width = 50;
    for (size_t i = 0; i < counts.size(); ++i) {
        double pct = counts[i] * 100.0 / total;
        int bar = int((pct * width / 100.0) + 0.5);
        std::cout << char('a' + i) << ": "
                  << std::setw(6) << counts[i] << " "
                  << std::fixed << std::setprecision(2)
                  << std::setw(6) << pct << "% "
                  << "|" << std::string(bar, '#') << "\n";
    }
}

Quick tips: convert letters to a single case before counting, ignore non-letter bytes, use an unsigned type (size_t/unsigned long) for counts, and always check total != 0 to avoid division by zero. To sort by frequency, build pairs (letter, count) and sort them while printing. For Unicode text, count code points rather than raw bytes and use an appropriate library.

Recommended Answers

All 2 Replies

Ok we have a breakthrough! I managed to do this after some digging into arrays :)

Now I'm trying to figure out how to normalize the views when printing

What exactly do you mean by normalize the views when printing. Can you copy and paste the source code so I can compile it and see what you mean. It sounds very interesting =) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.