Working with Ciphers

• (2 Pages)
• 1
• 2

27 Replies - 2089 Views - Last Post: 06 October 2010 - 08:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=193601&amp;s=0acf7226f3eef33bc9c40b252bd0e3d1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 kneil_20

• D.I.C Head

Reputation: 1
• Posts: 53
• Joined: 27-August 09

Working with Ciphers

Posted 05 October 2010 - 09:07 AM

its the last part of my project and I find it very hard I tried a lot of things but can't find a solution
So its conversion lets say:
equivalent code
a - 011
b - 11
c - 0001
d -10
e - 101

use is asked to input a code and it finds the equivalent
e.g
inputed: 1101110
output: bad

tried a lot but can't find a good algorithm for this
One of my attempts
```String conversion = "";
Scanner kbd = new Scanner(System.in);
System.out.print("\nEnter Code: ");
String input = kbd.nextLine();
while(input.length() != conversion.length()){
for(int x = 0; x < code.length; x++){
for(int y = 0; y < code.length; y++){
if(input.substring(0,code[y].length()).equals(code[x])){
conversion += input.substring(o,code[y];
}
}
}
System.out.println("Conversion: " + conversion);
}

```

Ah nvm the code I can't figure it out

This post has been edited by macosxnerd101: 05 October 2010 - 10:07 AM
Reason for edit:: Title renamed to be more descriptive. Please respect rule #4 of this forum. "I need help" isn't a descriptive title, as we know that's why you are here.

Is This A Good Question/Topic? 0

Replies To: Working with Ciphers

#2 KYA

• Wubba lubba dub dub!

Reputation: 3212
• Posts: 19,240
• Joined: 14-September 07

Re: Working with Ciphers

Posted 05 October 2010 - 09:21 AM

Are you given the codes ahead of time or are they defined by the user?
Was This Post Helpful? 0

#3 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

Re: Working with Ciphers

Posted 05 October 2010 - 09:34 AM

It seems to me that you might have to use a 26 argument switch statement, using case # for letter a. I think he wants the code inputted by the user.
Was This Post Helpful? 0

#4 kneil_20

• D.I.C Head

Reputation: 1
• Posts: 53
• Joined: 27-August 09

Re: Working with Ciphers

Posted 05 October 2010 - 09:37 AM

the codes are not given they can change.

the codes are not given they can change. Priority is alphabetical cause their might be instance that there are 2 or more possible solution but doing the code for the problem above will very much help me
Was This Post Helpful? 0

#5 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

Re: Working with Ciphers

Posted 05 October 2010 - 09:38 AM

kneil_20, on 05 October 2010 - 10:37 AM, said:

the codes are not given they can change.

the codes are not given they can change. Priority is alphabetical cause their might be instance that there are 2 or more possible solution but doing the code for the problem above will very much help me

You mean the code IS given by the user, and NOT already in the code. Correct?
Was This Post Helpful? 0

#6 kneil_20

• D.I.C Head

Reputation: 1
• Posts: 53
• Joined: 27-August 09

Re: Working with Ciphers

Posted 05 October 2010 - 09:39 AM

@jambr yes it's slightly true, actually its 32, some special characters included

to give more detail
```// not actual code just to show that this elements are in stored in arrays.
char [] c = {a,b,c,d,e};
String[] s = {011,11,0001,10,101};

```

This post has been edited by kneil_20: 05 October 2010 - 09:44 AM

Was This Post Helpful? 0

#7 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

Re: Working with Ciphers

Posted 05 October 2010 - 09:41 AM

No offence, but how can it be slightly true? Either I am right or I am wrong. Lol.
Was This Post Helpful? 0

#8 kneil_20

• D.I.C Head

Reputation: 1
• Posts: 53
• Joined: 27-August 09

Re: Working with Ciphers

Posted 05 October 2010 - 09:49 AM

the code varies whenever you add or remove something so i guess the code is changed by the user, but its not direct you don't choose what the equivalent will be but its dependent on your actions
Was This Post Helpful? 0

#9 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

Re: Working with Ciphers

Posted 05 October 2010 - 09:51 AM

Ok, you would just have to parse whatever the user enters (I assume they are entering the binary?) and make sure the computer recognizes when one letter stops and another one starts, other wise you will get some freaky-deaky shit.
Was This Post Helpful? -1

#10 kneil_20

• D.I.C Head

Reputation: 1
• Posts: 53
• Joined: 27-August 09

Re: Working with Ciphers

Posted 05 October 2010 - 10:10 AM

if you know the huffman code then here you go the right is the huffman code and the left is the character it represents.

if you know the huffman code then here you go the right is the huffman code and the left is the character it represents.
Was This Post Helpful? 0

#11 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

Re: Working with Ciphers

Posted 05 October 2010 - 10:12 AM

Yikes, I just Googled Huffman Coding and came up with an animation on Wikipedia, looks freaky.
Was This Post Helpful? 0

#12 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12769
• Posts: 45,954
• Joined: 27-December 08

Re: Working with Ciphers

Posted 05 October 2010 - 10:16 AM

kneil_20, on 05 October 2010 - 12:39 PM, said:

@jambr yes it's slightly true, actually its 32, some special characters included

to give more detail
```// not actual code just to show that this elements are in stored in arrays.
char [] c = {a,b,c,d,e};
String[] s = {011,11,0001,10,101};

```

Parallel arrays really aren't a good practice. A Map<Character, String> is a better choice of data structure.

Also, I found a good Huffman Compression tutorial you may find helpful.
Was This Post Helpful? 0

#13 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

Re: Working with Ciphers

Posted 05 October 2010 - 10:17 AM

Macosxnerd101 is always around to save the day.
Was This Post Helpful? 0

#14 kneil_20

• D.I.C Head

Reputation: 1
• Posts: 53
• Joined: 27-August 09

Re: Working with Ciphers

Posted 05 October 2010 - 10:23 AM

I actually have done the tree data structure came up with that huffman code left child : 1 right child: 0 I just need an algorithm to convert a huffman code into text which is freakingly hard for me
Was This Post Helpful? 0

#15 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12769
• Posts: 45,954
• Joined: 27-December 08

Re: Working with Ciphers

Posted 05 October 2010 - 10:26 AM

Still, use a Map and corresponding binary Strings of equal length for the varying characters. Then just break apart your String into units of length x and plug them into your Map.
Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }