Created
November 22, 2023 09:20
-
-
Save juanfal/fc2da5a3157f4a3af6fab9af5a2ad536 to your computer and use it in GitHub Desktop.
freq letter in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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