Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created November 22, 2023 09:20
Show Gist options
  • Save juanfal/fc2da5a3157f4a3af6fab9af5a2ad536 to your computer and use it in GitHub Desktop.
Save juanfal/fc2da5a3157f4a3af6fab9af5a2ad536 to your computer and use it in GitHub Desktop.
freq letter in a string
// t11e09.freqString.cpp
// juanfc 2023-11-22
//
#include <iostream>
#include <array>
using namespace std;
typedef array<int,256> TCharFreq;
TCharFreq getFreqs(string s);
void printFreqs(TCharFreq f);
int main()
{
printFreqs(getFreqs("telephone"));
return 0;
}
TCharFreq getFreqs(string s)
{
TCharFreq r;
for (int i = 0; i < 256; ++i)
r[i] = 0;
for (int i = 0; i < s.length(); ++i)
++r[s[i]];
return r;
}
void printFreqs(TCharFreq f)
{
for (int i = 0; i < 256; ++i)
if (f[i] > 0)
cout << char(i) << ": " << f[i] << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment