Computer Science Canada Turing encoding program |
Author: | jotapezm [ Wed Jan 13, 2016 6:10 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) |