Distribusi Frekuensi Huruf
Untuk mengetahui salah satu karakteristik suatu bahasa dapat digunakan cara menghitung distribusi frekuensi huruf. Frekuensi huruf akan memberikan informasi kepada kita bahasa yang dipakai. Distribusi frekuensi sangat berharga sebagai upaya untuk melakukan penyerangan terhadap suatu algoritma kriptografi. Algoritma kriptografi klasik banyak dipecahkan dengan bantuan distribusi frekuensi huruf.
Berikut adalah contoh program untuk memperoleh distribusi frekuensi suatu bahasa.

Gambar 2. Untuk mendapatkan distribusi frekuensi huruf dalam bahasa inggris tekan tombol "Count It".
Untuk mengetahui distribusi bahasa lainnya (terutama yang menggunakan huruf latin), masukan artikel berbahasa yang diinginkan kemudian tekan tombol “Count It”.
Contoh berikutnya adalah mencari distribusi frekuensi huruf dari bahasa Indonesia.
Source Code-nya adalah sbb :
// CharCounterDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CharCounter.h"
#include "CharCounterDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
struct dist_freq_huruf {
char hrf;
unsigned long freq;
} dist_freq[26];
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CCharCounterDlg dialog
CCharCounterDlg::CCharCounterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCharCounterDlg::IDD, pParent)
, m_txtInput(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCharCounterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, TXT_INPUT, m_txtInput);
}
BEGIN_MESSAGE_MAP(CCharCounterDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(BTN_CLOSE, &CCharCounterDlg::OnBnClickedClose)
ON_EN_CHANGE(TXT_INPUT, &CCharCounterDlg::OnEnChangeInput)
ON_BN_CLICKED(BTN_COUNT, &CCharCounterDlg::OnBnClickedCount)
END_MESSAGE_MAP()
// CCharCounterDlg message handlers
BOOL CCharCounterDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
UpdateData(TRUE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCharCounterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCharCounterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCharCounterDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCharCounterDlg::OnBnClickedClose()
{
OnCancel();
}
void CCharCounterDlg::OnEnChangeInput()
{
UpdateData(TRUE);
unsigned long len = m_txtInput.GetLength();
unsigned long TotalChar = 0;
CString CharsTot;
for (unsigned long i=0; i < len ; i++)
{
char ch = (char) m_txtInput.GetAt(i);
if (ch >= 'A' && ch <= 'Z') TotalChar++;
if (ch >= 'a' && ch <= 'z') TotalChar++;
}
CharsTot.Format(_T("%ld"), TotalChar);
SetDlgItemText(LBL_JMLCHAR, CharsTot);
}
void swap(dist_freq_huruf *x, dist_freq_huruf *y)
{
struct dist_freq_huruf temp;
temp.freq = x->freq;
temp.hrf = x->hrf;
x->freq = y->freq;
x->hrf = y->hrf;
y->freq = temp.freq;
y->hrf = temp.hrf;
}
int choose_pivot(int i,int j )
{
return((i+j) /2);
}
void quicksort(dist_freq_huruf *list, int m, int n)
{
int i,j,k;
struct dist_freq_huruf key;
if( m < n)
{
k = choose_pivot(m,n);
swap(&list[m], &list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i].freq <= key.freq)) i++;
while((j >= m) && (list[j].freq > key.freq)) j--;
if( i < j) swap(&list[i], &list[j]);
}
// swap two elements
swap(&list[m], &list[j]);
// recursively sort the lesser list
quicksort(list, m, j-1);
quicksort(list, j+1, n);
}
}
void CCharCounterDlg::OnBnClickedCount()
{
const int MAX_ELEMENTS = 26;
UpdateData(TRUE);
struct dist_freq_huruf dist_freq[26];
unsigned long len = m_txtInput.GetLength();
unsigned long Total[26];
CString CharsTot;
for (int i = 0; i < 26; i++)
{
dist_freq[i].hrf ='A' + i;
dist_freq[i].freq = 0;
Total[i] = 0;
}
for (unsigned long i=0; i < len ; i++)
{
char ch = (char) m_txtInput.GetAt(i);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
{
switch (ch)
{
case 'A':
case 'a':
dist_freq[0].freq += 1;
break;
case 'B':
case 'b':
dist_freq[1].freq += 1;
break;
case 'C':
case 'c':
dist_freq[2].freq += 1;
break;
case 'D':
case 'd':
dist_freq[3].freq += 1;
break;
case 'E':
case 'e':
dist_freq[4].freq += 1;
break;
case 'F':
case 'f':
dist_freq[5].freq += 1;
break;
case 'G':
case 'g':
dist_freq[6].freq += 1;
break;
case 'H':
case 'h':
dist_freq[7].freq += 1;
break;
case 'I':
case 'i':
dist_freq[8].freq += 1;
break;
case 'J':
case 'j':
dist_freq[9].freq += 1;
break;
case 'K':
case 'k':
dist_freq[10].freq += 1;
break;
case 'L':
case 'l':
dist_freq[11].freq += 1;
break;
case 'M':
case 'm':
dist_freq[12].freq += 1;
break;
case 'N':
case 'n':
dist_freq[13].freq += 1;
break;
case 'O':
case 'o':
dist_freq[14].freq += 1;
break;
case 'P':
case 'p':
dist_freq[15].freq += 1;
break;
case 'Q':
case 'q':
dist_freq[16].freq += 1;
break;
case 'R':
case 'r':
dist_freq[17].freq += 1;
break;
case 'S':
case 's':
dist_freq[18].freq += 1;
break;
case 'T':
case 't':
dist_freq[19].freq += 1;
break;
case 'U':
case 'u':
dist_freq[20].freq += 1;
break;
case 'V':
case 'v':
dist_freq[21].freq += 1;
break;
case 'W':
case 'w':
dist_freq[22].freq += 1;
break;
case 'X':
case 'x':
dist_freq[23].freq += 1;
break;
case 'Y':
case 'y':
dist_freq[24].freq += 1;
break;
case 'Z':
case 'z':
dist_freq[25].freq += 1;
break;
default:
break;
}
}
}
quicksort(&(*dist_freq), 0, MAX_ELEMENTS-1);
CharsTot.Format(_T("%ld"), dist_freq[25].freq);
SetDlgItemText(LBL_STATISTIKA, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[25].hrf);
SetDlgItemText(LBL_STATA, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[24].freq);
SetDlgItemText(LBL_STATISTIKB, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[24].hrf);
SetDlgItemText(LBL_STATB, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[23].freq);
SetDlgItemText(LBL_STATISTIKC, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[23].hrf);
SetDlgItemText(LBL_STATC, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[22].freq);
SetDlgItemText(LBL_STATISTIKD, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[22].hrf);
SetDlgItemText(LBL_STATD, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[21].freq);
SetDlgItemText(LBL_STATISTIKE, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[21].hrf);
SetDlgItemText(LBL_STATE, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[20].freq);
SetDlgItemText(LBL_STATISTIKF, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[20].hrf);
SetDlgItemText(LBL_STATF, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[19].freq);
SetDlgItemText(LBL_STATISTIKG, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[19].hrf);
SetDlgItemText(LBL_STATG, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[18].freq);
SetDlgItemText(LBL_STATISTIKH, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[18].hrf);
SetDlgItemText(LBL_STATH, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[17].freq);
SetDlgItemText(LBL_STATISTIKI, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[17].hrf);
SetDlgItemText(LBL_STATI, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[16].freq);
SetDlgItemText(LBL_STATISTIKJ, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[16].hrf);
SetDlgItemText(LBL_STATJ, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[15].freq);
SetDlgItemText(LBL_STATISTIKK, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[15].hrf);
SetDlgItemText(LBL_STATK, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[14].freq);
SetDlgItemText(LBL_STATISTIKL, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[14].hrf);
SetDlgItemText(LBL_STATL, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[13].freq);
SetDlgItemText(LBL_STATISTIKM, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[13].hrf);
SetDlgItemText(LBL_STATM, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[12].freq);
SetDlgItemText(LBL_STATISTIKN, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[12].hrf);
SetDlgItemText(LBL_STATN, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[11].freq);
SetDlgItemText(LBL_STATISTIKO, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[11].hrf);
SetDlgItemText(LBL_STATO, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[10].freq);
SetDlgItemText(LBL_STATISTIKP, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[10].hrf);
SetDlgItemText(LBL_STATP, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[9].freq);
SetDlgItemText(LBL_STATISTIKQ, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[9].hrf);
SetDlgItemText(LBL_STATQ, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[8].freq);
SetDlgItemText(LBL_STATISTIKR, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[8].hrf);
SetDlgItemText(LBL_STATR, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[7].freq);
SetDlgItemText(LBL_STATISTIKS, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[7].hrf);
SetDlgItemText(LBL_STATS, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[6].freq);
SetDlgItemText(LBL_STATISTIKT, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[6].hrf);
SetDlgItemText(LBL_STATT, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[5].freq);
SetDlgItemText(LBL_STATISTIKU, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[5].hrf);
SetDlgItemText(LBL_STATU, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[4].freq);
SetDlgItemText(LBL_STATISTIKV, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[4].hrf);
SetDlgItemText(LBL_STATV, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[3].freq);
SetDlgItemText(LBL_STATISTIKW, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[3].hrf);
SetDlgItemText(LBL_STATW, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[2].freq);
SetDlgItemText(LBL_STATISTIKX, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[2].hrf);
SetDlgItemText(LBL_STATX, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[1].freq);
SetDlgItemText(LBL_STATISTIKY, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[1].hrf);
SetDlgItemText(LBL_STATY, CharsTot);
CharsTot.Format(_T("%ld"), dist_freq[0].freq);
SetDlgItemText(LBL_STATISTIKZ, CharsTot);
CharsTot.Format(_T("%c"), dist_freq[0].hrf);
SetDlgItemText(LBL_STATZ, CharsTot);
}
Leave a Comment
