Character Counter
Dalam memecahkan suatu code yang di dalamnya ada pesan yang harus diterjemahkan maka kita perlu mengidentifikasi karakter apa saja yang sering muncul, karena banyaknya karakter yang sering muncul bisa kita gunakan sebagai petunjuk bahasa apa yang dipakai dalam pesan yang dikirimkan. Tugas ke 6 mata kuliah ini adalah membuat program counter jumlah karakter dari A sampai dengan Z sehingga kita bisa dapatkan jumlah tiap-tiap karakter dari naskah yang kita identifikasi.
Program yang dibuat menggunakan Javascript dan HTML sehingga diharapkan program bisa dicoba digunakan pada waktu tulisan ini diakses dengan browser.
Percobaan untuk teks dengan Bahasa Indonesia :
5 Karakter yang sering muncul : A, N, E, I dan S
Percobaan dengan teks Bahasa Inggris :
5 karakter yang sering muncul : E, T, N, I dan R
Untuk bahasa daerah (Bahasa Jawa) :
5 karakter yang sering muncul : O, A, N, I dan E
Code Program :
<!-- Script = by Bambang Supriyadi / 23208062 -->
<!-- Sekolah Teknik Elektro dan Informatika -->
<!-- Institut Teknologi Bandung -->
<!--
<textarea name="myMessage" onkeyup="ngitungHuruf(this.value)" rows="20" cols="100"></textarea>
<script language="JavaScript">
var cnt;
function ngitungHuruf(str) {
var a = 0;
var b = 0;
var c = 0;
var d = 0;
var e = 0;
var f = 0;
var g = 0;
var h = 0;
var i = 0;
var j = 0;
var k = 0;
var l = 0;
var m = 0;
var n = 0;
var o = 0;
var p = 0;
var q = 0;
var r = 0;
var s = 0;
var t = 0;
var u = 0;
var v = 0;
var w = 0;
var x = 0;
var y = 0;
var z = 0;
var ii = 0;
var array_jml_huruf = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
var array_jml_huruf_x = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
var array_huruf = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
"P","Q","R","S","T","U","V","W","X","Y","Z");
for(ii=0; ii<=str.length ;ii++){
stringx = str.charAt(ii);
if(stringx.toUpperCase() == "A"){
a++
array_jml_huruf[0] = a;
array_jml_huruf_x[0] = a;
tampil("a",a);
}
if(stringx.toUpperCase() == "B"){
b++
array_jml_huruf[1] = b;
array_jml_huruf_x[1] = b;
tampil("b",b);
}
if(stringx.toUpperCase() == "C"){
c++
array_jml_huruf[2] = c;
array_jml_huruf_x[2] = c;
tampil("c",c);
}
if(stringx.toUpperCase() == "D"){
d++
array_jml_huruf[3] = d;
array_jml_huruf_x[3] = d;
tampil("d",d);
}
if(stringx.toUpperCase() == "E"){
e++
array_jml_huruf[4] = e;
array_jml_huruf_x[4] = e;
tampil("e",e);
}
if(stringx.toUpperCase() == "F"){
f++
array_jml_huruf[5] = f;
array_jml_huruf_x[5] = f;
tampil("f",f);
}
if(stringx.toUpperCase() == "G"){
g++
array_jml_huruf[6] = g;
array_jml_huruf_x[6] = g;
tampil("g",g);
}
if(stringx.toUpperCase() == "H"){
h++
array_jml_huruf[7] = h;
array_jml_huruf_x[7] = h;
tampil("h",h);
}
if(stringx.toUpperCase() == "I"){
i++
array_jml_huruf[8] = i;
array_jml_huruf_x[8] = i;
tampil("i",i);
}
if(stringx.toUpperCase() == "J"){
j++
array_jml_huruf[9] = j;
array_jml_huruf_x[9] = j;
tampil("j",j);
}
if(stringx.toUpperCase() == "K"){
k++
array_jml_huruf[10] = k;
array_jml_huruf_x[10] = k;
tampil("k",k);
}
if(stringx.toUpperCase() == "L"){
l++
array_jml_huruf[11] = l;
array_jml_huruf_x[11] = l;
tampil("l",l);
}
if(stringx.toUpperCase() == "M"){
m++
array_jml_huruf[12] = m;
array_jml_huruf_x[12] = m;
tampil("m",m);
}
if(stringx.toUpperCase() == "N"){
n++
array_jml_huruf[13] = n;
array_jml_huruf_x[13] = n;
tampil("n",n);
}
if(stringx.toUpperCase() == "O"){
o++
array_jml_huruf[14] = o;
array_jml_huruf_x[14] = o;
tampil("o",o);
}
if(stringx.toUpperCase() == "P"){
p++
array_jml_huruf[15] = p;
array_jml_huruf_x[15] = p;
tampil("p",p);
}
if(stringx.toUpperCase() == "Q"){
q++
array_jml_huruf[16] = q;
array_jml_huruf_x[16] = q;
tampil("q",q);
}
if(stringx.toUpperCase() == "R"){
r++
array_jml_huruf[17] = r;
array_jml_huruf_x[17] = r;
tampil("r",r);
}
if(stringx.toUpperCase() == "S"){
s++
array_jml_huruf[18] = s;
array_jml_huruf_x[18] = s;
tampil("s",s);
}
if(stringx.toUpperCase() == "T"){
t++
array_jml_huruf[19] = t;
array_jml_huruf_x[19] = t;
tampil("t",t);
}
if(stringx.toUpperCase() == "U"){
u++
array_jml_huruf[20] = u;
array_jml_huruf_x[20] = u;
tampil("u",u);
}
if(stringx.toUpperCase() == "V"){
v++
array_jml_huruf[21] = v;
array_jml_huruf_x[21] = v;
tampil("v",v);
}
if(stringx.toUpperCase() == "W"){
w++
array_jml_huruf[22] = w;
array_jml_huruf_x[22] = w;
tampil("w",w);
}
if(stringx.toUpperCase() == "X"){
x++
array_jml_huruf[23] = x;
array_jml_huruf_x[23] = x;
tampil("x",x);
}
if(stringx.toUpperCase() == "Y"){
y++
array_jml_huruf[24] = y;
array_jml_huruf_x[24] = y;
tampil("y",y);
}
if(stringx.toUpperCase() == "Z"){
z++
array_jml_huruf[25] = z;
array_jml_huruf_x[25] = z;
tampil("z",z);
}
}
//document.write(array_jml_huruf + "<br />");
var array_jml_huruf_urut = new Array(5);
array_jml_huruf_urut = array_jml_huruf.sort(doCompare);
array_jml_huruf_urut = array_jml_huruf_urut.reverse();
array_jml_huruf_urut = array_jml_huruf_urut.slice(0,5);
var array_huruf_terbesar = new Array(5);
//document.write(array_jml_huruf_x + "<br />");
for(var xx=0 ; xx < array_jml_huruf_urut.length ; xx++){
for(var yy=0 ; yy < array_jml_huruf_x.length ; yy++){
//document.write(yy+"----"+array_jml_huruf_urut[xx]+"--------"+array_jml_huruf_x[yy] + "<br />");
if(array_jml_huruf_urut[xx] == array_jml_huruf_x[yy]){
array_huruf_terbesar[xx] = array_huruf[yy];
}
}
}
tampil("array_huruf_terbesar",array_huruf_terbesar);
}
function doCompare(a, b) {
return a - b
}
function tampil(chr,val) {
var aa = document.getElementById(chr);
aa.value = val;
}
document.write("<br>A : <input type=text id=a size=4 readonly> B : <input type=text id=b size=4 readonly> C : <input type=text id=c size=4 readonly> D : <input type=text id=d size=4 readonly> E : <input type=text id=e size=4 readonly> F : <input type=text id=f size=4 readonly> G : <input type=text id=g size=4 readonly> H : <input type=text id=h size=4 readonly> I : <input type=text id=i size=4 readonly> J : <input type=text id=J size=4 readonly>");
document.write("<br>K : <input type=text id=k size=4 readonly> L : <input type=text id=l size=4 readonly> M : <input type=text id=m size=4 readonly> N : <input type=text id=n size=4 readonly> O : <input type=text id=o size=4 readonly> P : <input type=text id=p size=4 readonly> Q : <input type=text id=q size=4 readonly> R : <input type=text id=r size=4 readonly> S : <input type=text id=s size=4 readonly> T : <input type=text id=t size=4 readonly>");
document.write("<br>U : <input type=text id=u size=4 readonly> V : <input type=text id=v size=4 readonly> W : <input type=text id=w size=4 readonly> X : <input type=text id=X size=4 readonly> Y : <input type=text id=y size=4 readonly> Z : <input type=text id=z size=4 readonly>");
document.write("<br>5 Character yang sering muncul : <input type=text id=array_huruf_terbesar size=10 readonly>");
</script>


0 Comments:
Post a Comment
<< Home