Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Secret Code Language: need help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xXInsanityXx




PostPosted: Wed Nov 02, 2005 9:16 pm   Post subject: Secret Code Language: need help

Alright... Hi, I wonder if anyone can help me figure out how to solve my problem. To begin with let me tell you my problem:

I have to make a secret message decoder in Turing, and the way to decode my message is to find each letter on the keyboard and find the key that is to its left to get the actual key value, for example: if you get some thing like EJSY which is in secret code. Then the actual message would be WHAT

if you still don't understand let me make it simple for you:
EJSY would be in secret code language, and it contains a secret message (word in english) and your job is to decode it (make a turing program to do it for you). Each letter in the secret code is substituted to an actual letter in the English alphabets.

In order to find this substituted letter you have to map your keyboard (This requires a bit of eye coordination). For example in order to translate the secret code letter D first we have to look at our keyboard, then we have to find the key to its left in this case it would be S, and replace D with the letter S. In the case where the letters are located at the atmost left of the keyboard and has no other letter next to it, then we find the letter to the atmost right on the same row of the keyboard for example if you get the letter A in secret code and you have to translate it, then the ACTUAL letter in English would be L, an other example would be the letter Q, the replacement for the letter Q would be the letter P.

Here is an Example:

seperate the letters in E J S Y in this E substitutes W, J substitutes H, S substitutes A, and Y substitutes T. In order to find the English word which is W H A T



Hopefully you understand the logic now, i would like to know your suggestions, on how to do this in the most efficient and simple way.

If you have any problems with the logic or anything else please feel free to ask any questions.

And i have to make this so that it does sentences (which include spacing) for example: EJSY YJW JWVL = WHAT THE HECK
Sponsor
Sponsor
Sponsor
sponsor
codemage




PostPosted: Thu Nov 03, 2005 8:52 am   Post subject: (No subject)

It depends on how you're getting input (are you outputting the message as it is typed, or do you get it as a full string...?).

At some point, you'll have to break down the input by characters.
Then substitute each character for the proper one.
If you're grade 10ish, you'll probably want to use an IF or CASE statement for that.
xXInsanityXx




PostPosted: Fri Nov 04, 2005 6:05 am   Post subject: (No subject)

No, I am not outputting it as its being typed, it has to get the whole input first then output the decoded message. I just want to know how would this be done most efficiently? and KISSed as well
Tony




PostPosted: Fri Nov 04, 2005 9:54 am   Post subject: (No subject)

have two arrays - one for the keyboard layout ["Q", "W", "E", "R"] and one for the map ["P", "Q", "W", "E"].

You find the location of the letter on the layout ("E" is 3rd) and replaces it with the equivalent from the map (3rd letter is "W").
Cervantes




PostPosted: Fri Nov 04, 2005 2:02 pm   Post subject: (No subject)

It could also be done using some string manipulation. Create 3 strings: "qwertyuiop", "asdfghjkl", and "zxcvbnm". Alternatively, you could create 1 string, but the coder/decoder would function slightly differently: "qwertyuiopasdfghjklzxcvbnm". Use index() to find you real letter in those strings, then replace it with the string (1 + position of real character). Repeat for all characters. It will be a little more complex when you have 3 strings representing the keyboard, but doable nonetheless. Hint: Make the 3 strings into 1 array. var keyboard : array 1 .. 3 of string := init ("qwertyuiop", "asdfghjkl", "zxcvbnm")
MysticVegeta




PostPosted: Fri Nov 04, 2005 6:08 pm   Post subject: (No subject)

why do you need arrays?
code:
var num : string := "qwertyuiopasdfghjklzxcvbnm"
var input : string
var out : string := ""
get input
for x : 1 .. length (input)
    if input (x) = "q" then
        out += "p"
    elsif input (x) = "a" then
        out += "l"
    elsif input (x) = "z" then
        out += "m"
    else
        out += num (index (num, input (x)) - 1)
    end if
end for
put out

xXInsanityXx




PostPosted: Fri Nov 04, 2005 11:42 pm   Post subject: (No subject)

Thanks a lot for your insights and advice, Mystic... i said i would need whole sentences, but your code wasnt bad. I read AsianSensations tutorial on the index command, and to my suprise it had EXACTLY what i needed. So here is the code tell me if anything is wrong... this is the basic code and converts english words into the secret code, and i will also submit one that converts the secret code to english words.

CODE FOR ENGLISH TO SECRET LANGUAGE:
code:
var word : string
get word : *

var alpha := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
var alpha2 := "SNVFRGHJOKLAZMPQWTDYIBECUXsnvfrghjoklazmpqwtdyibecux "

for con : 1 .. length (word)
    if index (alpha, word (con)) not= 0 then
        put alpha2 (index (alpha, word (con))) ..
    else
        put word (con)
    end if
end for


CODE FOR SECRET LANGUAGE TO ENGLISH:

code:
var word : string
get word : *

var alpha2 := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
var alpha := "SNVFRGHJOKLAZMPQWTDYIBECUXsnvfrghjoklazmpqwtdyibecux "

for con : 1 .. length (word)
    if index (alpha, word (con)) not= 0 then
        put alpha2 (index (alpha, word (con))) ..
    else
        put word (con)
    end if
end for


Well the second code is basically the same thing as the first one except that the string value of the alpha and alpha2 var have been switched.

Thanks for all of your help... Very Happy, and please feel free to comment or state any changes that will KISS the code even more. Thank you again
Cervantes




PostPosted: Sat Nov 05, 2005 6:55 am   Post subject: (No subject)

MysticVegeta wrote:
why do you need arrays?


I never said you did. I gave three options, two of which did not use arrays.

MysticVegeta: Notice how 3 people posted replies without giving the answer away? There was a point to that.

Anyways, since you've got the basic code: http://www.compsci.ca/v2/viewtopic.php?t=9661. With particular attention to the second page.

You also might want to check out ord() and chr(). Described in String Manipulation tutorial.
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Thu Nov 10, 2005 3:06 pm   Post subject: (No subject)

Sorry for the late reply, I forgot that i had posted about this problem before. I am getting old you know Laughing lol just kidding.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: