programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Hi guys, how is it going?!

In short, I've got a cipher code, and all I need to do is to:

1-count the occurrences for each letter
2-replace the most frequent letter with 'E', the second with 'T' and so on.

Fairly simple!

However, what I have done is :

so, my problem is that once I finish counting how many times each letter appeared, I don't know what to do next because of the changing of 'freq' array, which's got each index corresponds to an alphabetic one (0,A),(1,B)...and so on

please enlighten me to improve this code

Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
Hi,
Welcome to javaranch. This prolem was discussed in the forum several times, so I advice you to do a search on the subject here in the forum.
Think a little bit about the sorting part. You stored the occurence count for all the letters in your array. You know that freq[0] belongs to Alpha[0], freq[1] belongs to Alpha[1], ....
After counting you reorder the freq array. You missed one thing here. What do you want to achieve by reordering the array? Think about it and you will see what I'm talking about.

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Thanks Miklos

I believe that you're talking about I have missed each letter number of occurrences !?
This is my nightmare that I could do!

Miklos Szeles
Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
I mean, you sort the freq array to order the letters based on the count of their occurences which is right. But you just mix up the freq array and forgot to order the alphabet according to that.

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
WOW! I never thought of that!
I'll have a go, thank you

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
I give up

any further hints?

Miklos Szeles
Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
You give up really easy. Forget about Java, just concentrate on the algorithm. Write down the algorithm, and when it's okay you can start to implement it in Java. We can help with that,but we won't give the soultion instead of you;)

Marshal
Posts: 79231
377
• Number of slices to send:
Optional 'thank-you' note:

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
I really appreciate your collaboration, and believe me I'm not looking for a ready-solution ..and to show my good intentions let me tell you what I am thinking about,algorithm-wise not java:

1-Create a two-dimensional array arr[26][2], the first column is for holding indices from 0-25, and the other one is holding the frequencies for each letter taking the values from "freq" array ,e.g: a[0][0]=0-->A & a[0][1]= 10 times of occurrences

2-After that we sort the 2-dimensional array according to the 2nd column which holds the occurrences number, so we still hold the index for every letter,and easily we know where the 'A' went and other letters

3-Now I got lost!!

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:And if you want answers, in future please note this FAQ.

Sorry, cheer up mate

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
This what I could come up with! it didn't work though!

Campbell Ritchie
Marshal
Posts: 79231
377
• Number of slices to send:
Optional 'thank-you' note:
Please have a look at the administrative private message I sent a minute or so ago.

[Pedantic mode]There is no such thing as a 2D array in Java. You only get arrays of arrays, which are more versatile[/Pedantic mode]

You only need one array for counting frequencies, if you are using arrays. No need for two arrays. You simply need a frequency array, and you can increment the elements as you pass the letters. That bit is really easy. You probably don't need the ABCD... array either.

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:Please have a look at the administrative private message I sent a minute or so ago.

[Pedantic mode]There is no such thing as a 2D array in Java. You only get arrays of arrays, which are more versatile[/Pedantic mode]

You only need one array for counting frequencies, if you are using arrays. No need for two arrays. You simply need a frequency array, and you can increment the elements as you pass the letters. That bit is really easy. You probably don't need the ABCD... array either.

Thanks a lot, the counting bit is easy, but my problem still in how I can hold the index of each frequency! in order to replace its corresponding letter

Campbell Ritchie
Marshal
Posts: 79231
377
• Number of slices to send:
Optional 'thank-you' note:
I take your point. You need some way to link letters to frequencies.

Set up a class which incorporates a letter, a frequency and a replacement letter. Put those in the array from A to Z. Remember a char is not a character at all, despite what people say. It is a number. You can do arithmetic on it. Try this:So you can use each letter to access the array and call its increment() method.
Make your class implement the Comparable interface, and simply subtract the two frequencies in the compareTo() method.
Then set the replacement character after sorting, and comparison with the frequencies. According to this Oxford English Dictionary website, that is EARIOTNSLCUDPMHGBFYWKVXZJQ. But you have been given a different set of frequencies, which you had best use.
Then use that replacement in your original String.

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:I take your point. You need some way to link letters to frequencies.

Set up a class which incorporates a letter, a frequency and a replacement letter. Put those in the array from A to Z. Remember a char is not a character at all, despite what people say. It is a number. You can do arithmetic on it. Try this:So you can use each letter to access the array and call its increment() method.
Make your class implement the Comparable interface, and simply subtract the two frequencies in the compareTo() method.
Then set the replacement character after sorting, and comparison with the frequencies. According to this Oxford English Dictionary website, that is EARIOTNSLCUDPMHGBFYWKVXZJQ. But you have been given a different set of frequencies, which you had best use.
Then use that replacement in your original String.

I really appreciate your hints, but I still can't do it!
Especially I don't know what to compare with each other!?

Campbell Ritchie
Marshal
Posts: 79231
377
• Number of slices to send:
Optional 'thank-you' note:
I tried it last night with both the frequency tables on that Oxford website, and got a decrypted output . . . and couldn't understand it

If you want to get the index of your 26-element array, try myLetters['z' - 'a'].
Create a simple method which demonstrates an array like that.
Then go through your coded String, and for each of the letters, increment the count in your array. You can even iterate through the array like thisSee if you can get that bit working. See whether that helps at all. Note you can use upper-case and lower-case versions of that for loop.

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:I tried it last night with both the frequency tables on that Oxford website, and got a decrypted output . . . and couldn't understand it

If you want to get the index of your 26-element array, try myLetters['z' - 'a'].
Create a simple method which demonstrates an array like that.
Then go through your coded String, and for each of the letters, increment the count in your array. You can even iterate through the array like thisSee if you can get that bit working. See whether that helps at all. Note you can use upper-case and lower-case versions of that for loop.

If you really interested in knowing the whole thing , here you go:
ZYEMOUSOUSIZWYIXOKSOTZQSOBSYLSWDKHTMOWARMSKSRRSILISFESWBHSTOISQISRRZWSOIRYRMSOUSIODSTLYISWDKHTMOWAWYEWBYXXYWSWDKHTMGYIATOISQISTSWRRMSYRMSIRSVRTGHKKWYRJSTYSOTZOWAXOZJSMOIARYAYJISONHWDRMHTBHQMSIJZMOWATMYEKAWYRJSAHLLHBEKRRYAYMYGSUSIRMHTSVSIBHTSOTNTZYERYGIHRSOQIYDIOXRMORORROBNTBHQMSITOERYXORHBOKKZOWAJISONTRMSXGHRMYEROETSIMOUHWDRYPEXQHWOWAXONSOBMOWDSOTHXQKSJIERSLYIBSOQQIYOBMHTWYRWSBSTTOIHKZRMSWHBSTRGOZRYAYWSRMHTRMSRIHBNRYAYHWDRMHTGHKKJSRYASRSBRGMSWRMSTEJTRHRERHYWHTWYRIHDMROWARYLHWAOJSRRSIYWSETHWDRMSLISFESWBHSTRYDEHASZYEDYYAKEBN

and thanks for the help, I'll try it

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

When you swap the freqs, shouldn't you swap the letters too? After all, the letter and freqs should stay together right?

Henry

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:

Henry Wong wrote:

When you swap the freqs, shouldn't you swap the letters too? After all, the letter and freqs should stay together right?

Henry

Thanks a lot, I should not miss that!

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
I just wanna say one thing, programming require patient a lot, keep going

Abdulmalik Malik
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:

lisa broker wrote:I just wanna say one thing, programming require patient a lot, keep going

Exactly, and this what I lack, I'm afraid!
But by staying in touch, with people as such, I should become patient very much!

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.