Turing encoding program
Author |
Message |
jotapezm
|
Posted: Wed Jan 13, 2016 6:11 pm Post subject: Turing encoding program |
|
|
I have created a program which takes a text file, encodes it, prints it into another text file. After that you run the decoding program to input the encoded text file and it decodes back to the original text.
My decoding procedure is not working, it is not successfully decoding the text file.
Encoding program :
% Encryption
var stream : int
var sWord : string
const dictionary := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const numbers := "1234567890"
var key : string := "123"
var FileName : string
function encrypt (sWord, key : string) : string
var s : string
var news := ""
s := sWord
for i : 1 .. length (s)
if index (dictionary, s (i)) > 0 then
news += chr (65 +(((ord (s (i)) + 65) + (ord (key (i mod length (key) + 1)) - 65)) mod 26))
elsif index (numbers, s (i)) > 0 then
news += chr (49 +(((ord (s (i)) + 49) + (ord (key (i mod length (key) + 1)) - 49)) mod 10))
else
news += s (i)
end if
end for
result news
end encrypt
procedure DisplayCharss (FileName : string)
var stream2 : int
var stream : int
open : stream, FileName, get
open : stream2, "encoded.txt", put
if stream > 0 then
var sWord : string
loop
%input from file
get : stream, skip
exit when eof (stream)
get : stream, sWord : *
sWord := encrypt (sWord, key)
put : stream2, sWord
put sWord
end loop
close : stream
close : stream2
else
put "Error could not open file"
end if
end DisplayCharss
put " input file name"
get FileName
DisplayCharss (FileName)
Decoding program :
const dictionary := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuwxyz"
const numbers := "1234567890"
var stream : int
var key : string := "123"
var FileName : string
var sWord : string
function decrypt (sWord, key : string) : string
var news := ""
var s := sWord
for i : 1 .. length (s)
if index (dictionary, s (i)) > 0 then
news += chr (65 + (((ord (s (i)) +65) -(ord (key (i mod length (key) + 1)) +65)) mod 26))
elsif index (numbers, s (i)) > 0 then
news += chr (49 + (((ord (s (i)) + 49)-(ord (key (i mod length (key) + 1)) + 49)) mod 10))
else
news += s (i)
end if
end for
result news
end decrypt
procedure DisplayCharss (FileName : string)
var stream2 : int
var stream : int
open : stream, FileName, get
open : stream2, "decoded.txt", put
if stream > 0 then
var sWord : string
loop
%input from file
get : stream,skip
exit when eof (stream)
get : stream, sWord : *
sWord := decrypt (sWord, key)
put : stream2, sWord
put sWord
end loop
close : stream
close : stream2
else
put "Error could not open file"
end if
end DisplayCharss
put " input file name"
get FileName
DisplayCharss (FileName) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dreadnought
|
Posted: Fri Jan 15, 2016 9:24 pm Post subject: Re: Turing encoding program |
|
|
In the future, you should wrap your code like this
code: | [syntax="Turing"]
Your code here
[/syntax] |
to help with readability.
Your main problems all occur on the lines that look like (in both the encoding and decoding programs)
Turing: | news + = chr (65 + (((ord (s (i )) + 65) + (ord (key (i mod length (key ) + 1)) - 65)) mod 26)) |
Think carefully about the logic here (hint: one or more +'s should be -'s or vice versa). This should fix most of your decoding.
To fix the rest, the ASCII standard (the standard that defines which letters correspond to which numbers) characters 48 to 57 are "0123456789" and characters 65 to 122 are "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz". The important things to notice are that 0 appears before 1 and that there are non-alphabetic characters between Z and a.
I also noticed that you like to use values like 49 and 65 in your program. You might want to consider writing these using ord such as ord('A') for 65. This will make your code more readable, and make it harder to enter the wrong number. |
|
|
|
|
|
|
|