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

Username:   Password: 
 RegisterRegister   
 encrypting codes
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
uknowhoiam




PostPosted: Thu Dec 18, 2008 8:27 pm   Post subject: encrypting codes

my question that i have to answer is this

. Write a program to read in a phrase and encrypt this phrase using a basic ?Caesar? cipher. Caesar realized that messages could be hidden in encrypted text. He sent messages by shifting characters to encrypt the messages contents. Your program will read in a phrase and encrypt the phrase. The user will choose the character shift or a random number can be chosen for the character shift (between 1 and 25) .The original and encrypted phrases must be printed out.

This is what i got so far..
Turing:

var phrase : string := ""
var number, char_shift, word, encrypt : int := 0


put "Enter a phrase"
get phrase : *
put "Enter a character shift"
get char_shift

% This loop will repeat untill the end of the phrase
for count : 1 .. length (phrase)

    encrypt := encrypt - 1
    % Encrypt will equal to the phrase in an integer value

    encrypt := ord (phrase (count))
    % if encrypt is 'space' then ..

    if encrypt = 32 then
        % encrypt will be subtracted by char_shift

        encrypt := encrypt - char_shift
        % If encrypt is capital z then..
    elsif encrypt = 90 then
        % encrypt will go back and will be before A

        encrypt := 64
        % If encrtpy is z then..
    elsif encrypt = 122 then
        %encrypt will go back and will be before a
        encrypt := 96
    end if
    %word will equal encrypt and will add the char_shift
    word := encrypt + char_shift
    put chr (word) ..
end for


my problem is that, when encrypt is over lowercase z then it goes further in the ascii code (w/e is after z)
what i want is that i want it to go to capital A instead of going further

im stuck Sad anyone want to help me Smile

Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="Turing"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Dec 18, 2008 8:57 pm   Post subject: RE:encrypting codes

perhaps 'if ord (letter) > ord(z) then however many characters it is past z + A'?. That made sense to me, but I doubt I explained it well.

You need to find out how far a letter is past ascii z and then add that much -1 to ascii A. So say it is at ascii 124, it is 2 more than z, so add 2-1 to A, to get a capital B. Remember that there is a few ascii characters between Z and a.

Oh, and Turing uses ansii (not that it makes too much difference).
uknowhoiam




PostPosted: Fri Dec 19, 2008 3:53 pm   Post subject: RE:encrypting codes

ugh i handed my program in so now it doesnt matter if i edit it, but my teacher said it doesnt really matter if i get this question wrong cuz it is really hard, she wont cut marks off
syntax_error




PostPosted: Fri Dec 19, 2008 8:34 pm   Post subject: RE:encrypting codes

Now you have 2 weeks(ish) to fix it, and learn if you wish to.
Tyr_God_Of_War




PostPosted: Fri Dec 19, 2008 9:10 pm   Post subject: Re: encrypting codes

Turing:



    elsif encrypt >= 90 then
        % encrypt will go back and will be before A

        encrypt :=encrypt-26


Would that work?
SNIPERDUDE




PostPosted: Sat Dec 20, 2008 10:46 am   Post subject: Re: encrypting codes

Tyr_God_Of_War @ December 19th 2008 wrote:
Turing:



    elsif encrypt >= 90 then
        % encrypt will go back and will be before A

        encrypt :=encrypt-26


Would that work?


You could of just as easily made this line
Turing:
encrypt := encrypt - 26

this
Turing:
encrypt -= 26


for future reference. You just put the character before the equal so you don't have to repeat the variable name. So for example multiplying the variable Test by 3 would look like this:
Turing:
Test *= 3
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  [ 6 Posts ]
Jump to:   


Style:  
Search: