Monday, April 23, 2012

Program C mengurutkan frekuensi karakter dari string

Tugas lagi, tugas lagi.. sampai kurang tidur.
Jadi tugasnya begini :
Buatlah program untuk menampilkan jumlah karakter-karakter yang digunakan pada suatu teks. Urutkan karakter tersebut dari yang
muncul paling sering ke karakter yang paling sedikit muncul.

char *teks[]={   
"In this age of viruses and hackers, of electronic eavesdropping and electronic fraud, security is paramount.",
"As the disciplines of cryptography and network security have matured, more practical, readily available",
"applications to enforce network security have developed. This text provides a practical survey of both the",
"principles and practice of cryptography and network security. First, the basic issues to be addressed by a",
"network security capability are explored through a tutorial and survey of cryptography and network security technology."
}

Ini source code saya :
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

struct list{
       char huruf;
       int frekuensi; };

int main() {
struct list listke[26],temp;
int totalkarakter=0, counter,a,b,c;

char *teks[]={   
"In this age of viruses and hackers, of electronic eavesdropping and electronic fraud, security is paramount.",
"As the disciplines of cryptography and network security have matured, more practical, readily available",
"applications to enforce network security have developed. This text provides a practical survey of both the",
"principles and practice of cryptography and network security. First, the basic issues to be addressed by a",
"network security capability are explored through a tutorial and survey of cryptography and network security technology."
};
//mengeset nilai frekuensi awal=0
for(c=0;c<26;c++){
                  listke[c].frekuensi=0;};
                 


for(b=0;b<5;b++){//pengulangan untuk jumlah teks
counter=strlen(teks[0]);
totalkarakter=totalkarakter+counter;
for(c=0;c<26;c++){//pengulangan untuk karakter
for(a=0;a<counter;a++){//pengulangan untuk mengetahui frekuensi karakter
                       if(teks[b][a]=='a'+c){listke[c].huruf='a'+c;listke[c].frekuensi++;};
};
};
};

//pengurutan
for(a=0;a<(26-1);a++){
                         for(b=a;b<26;b++){
                         if(listke[b].frekuensi>listke[a].frekuensi){
                                               temp=listke[a];
                                               listke[a]=listke[b];
                                               listke[b]=temp; };                   
                                               };
                                               }

printf("total karakter adalah = %d karakter",totalkarakter);
for(c=0;c<26;c++){if(listke[c].frekuensi!=0){
                  printf("\nkarakter %c frekuensi=%d",listke[c].huruf,listke[c].frekuensi);}};;
                 
    printf("\n");
    system("pause");
}


   



kalau ada yang salah komen ya..

my life's suck lately n it's getting worse!

0 comments:

Post a Comment