Help With Caesar Cipher/Shift

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 19
Reputation: JJarvis is an unknown quantity at this point 
Solved Threads: 0
JJarvis's Avatar
JJarvis JJarvis is offline Offline
Newbie Poster

Help With Caesar Cipher/Shift

 
0
  #1
Jun 20th, 2007
Well, hi, I wanted to make a program, which turns normal words into a Caesar shift.

So...

HELLO

BECOMES;

JGNNW

That is with a shift of two.

I did not want to include IF statements in this, as that is 'lazy programming'

I have made a program which will convert one letter, but no more...

So... say, I typed A in, C would come out.

But, if I typed ABC in a really wierd symbol would come out...

The way that this works is by taking A, converting it into it's ASCII code (65) adding two (67) and then converting back into letter form...

So, a summary, I just want this code to be adapted so that it will convert more than one letter, I think I will need to use a loop, but I am unsure on how to implement this into my code, as I have not been doing programming for very long...

Pascal and Delphi Syntax (Toggle Plain Text)
  1.  
  2. Program shift;
  3.  
  4. VAR
  5.  
  6. asciicode, number : Integer;
  7. letter, asciiletter, newcode : Char;
  8.  
  9. Procedure input;
  10.  
  11. BEGIN
  12. Writeln ('Enter a word');
  13. readln(letter);
  14. end;
  15.  
  16. Procedure convert;
  17.  
  18. BEGIN
  19. asciicode := ord(letter);
  20. asciiletter := chr(number);
  21. newcode := chr(asciicode+2);
  22.  
  23. end;
  24.  
  25. Procedure output;
  26.  
  27. BEGIN
  28. Writeln ('The ASCII code of ',letter,' is ',asciicode);
  29. Writeln (newcode);
  30. Readln;
  31. end;
  32.  
  33. BEGIN
  34.  
  35. input;
  36. convert;
  37. output;
  38.  
  39. END.
There are 10 people in the world, those that understand binary and those that don't.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 27
Reputation: Terry Robinson is an unknown quantity at this point 
Solved Threads: 1
Terry Robinson Terry Robinson is offline Offline
Light Poster

Re: Help With Caesar Cipher/Shift

 
0
  #2
Jun 20th, 2007
You are on the right track, and yes you need a loop. However, whoever told you that using IF was lazy programming has no idea what they are talking about! Conditional execution is one of the cornerstones of programming. Example: what happens if you enter Y or Z? You go beyond the end of the ASCII uppercase alphabet. My idea is to cycle back to the start in that case so that ABCXYZ becomes CDEZAB and so on, and that requires use of IF.

Anyway, all you need is a loop to iterate over the characters in a string, convert them and put them back (looks like you need to read up on string use too)

Pascal and Delphi Syntax (Toggle Plain Text)
  1.  
  2. procedure Convert(var s :string);
  3. var
  4. i :integer;
  5. begin
  6. for i := 1 to Length(s) do
  7. if Ord(s[i]) >= Ord('Y') then
  8. s[i] := Chr(Ord(s[i])-24)
  9. else
  10. s[i] := Chr(Ord(s[i])+2);
  11. end;

Also note that you can use the result of Ord and Chr without storing them in intermediate variables.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 19
Reputation: JJarvis is an unknown quantity at this point 
Solved Threads: 0
JJarvis's Avatar
JJarvis JJarvis is offline Offline
Newbie Poster

Re: Help With Caesar Cipher/Shift

 
0
  #3
Jun 21st, 2007
*Head explodes* But you can just make it so that the number will go through a loop too... and I still know that there should be a way to do it without IF statements...

Oh... and I've been trying programming for about a year... but I am only 12...
There are 10 people in the world, those that understand binary and those that don't.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 27
Reputation: Terry Robinson is an unknown quantity at this point 
Solved Threads: 1
Terry Robinson Terry Robinson is offline Offline
Light Poster

Re: Help With Caesar Cipher/Shift

 
0
  #4
Jun 21st, 2007
Originally Posted by JJarvis View Post
*Head explodes* But you can just make it so that the number will go through a loop too... and I still know that there should be a way to do it without IF statements...

Oh... and I've been trying programming for about a year... but I am only 12...
You only need the IF statement in this case if you want to keep all of the output as all letters. If that doesn't matter, sure, you can just run the loop directly and eliminate the first part of the IF in my example but letters at the end of the alphabet will end up as the characters which come after the letters in ASCII.

However, I still question why you are so interested in avoiding IF. It is one of the most important statements in any programming language, and something you will need to use in every program you ever write. When you think about it, even your loop is a form of an IF statement ("if we're not done, repeat this loop") If you try to find a way to write all of your programs without using IF you are going to have a very hard time of it.

Are you perhaps confusing IF with GOTO? That statement (which isn't even available in some languages) is indeed considered bad programming and is best avoided except in special circumstances.

Anyway, good luck, hope this helps a little.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 19
Reputation: JJarvis is an unknown quantity at this point 
Solved Threads: 0
JJarvis's Avatar
JJarvis JJarvis is offline Offline
Newbie Poster

Re: Help With Caesar Cipher/Shift

 
0
  #5
Jun 21st, 2007
Well... I may want an IF statement, but I do not want it so that you go;

if a = B then
writeln ('D');
There are 10 people in the world, those that understand binary and those that don't.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 1
Reputation: pichi is an unknown quantity at this point 
Solved Threads: 0
pichi pichi is offline Offline
Newbie Poster

Re: Help With Caesar Cipher/Shift

 
0
  #6
Nov 2nd, 2007
I want the coding for other program:
Write an applet class which construct a substitution alphabet to use as a cipher as follows: Use a keyword to construct the alphabet. For example, suppose the keyword is "zebra". Place this keyword at the beginning of the alphabet, and then fill in the other 21 slots with the remaining letters from the alphabet.



Ciper alphabet: zebracdfghijklmnopqstuvwxy

Plain alphabet: abcdefghijklmnopqrstuvwxyz



Now in the cipher text, the letter a is substituted by letter z, the letter b is substituted by letter e etc.



This class, called alphabet, should have a public method called cipher Alphabet() which is passed the keyword as a String and returns an alphabet string. +cipher Alphabet(String s): String.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help With Caesar Cipher/Shift

 
0
  #7
Nov 2nd, 2007
I didn't know you could make applets using Pascal... Are you sure you aren't using Java or somesuch?

Either way, remember that your substitution cipher is composed of two separate pieces of information: the code word and the remaining alphabet:
code word + remaining alphabet
You can consider each part separately.

To create the remaining alphabet, just say the alphabet but leave out the letters found in the code word.

A thought: what happens if your code word has duplicates?

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum


Views: 3988 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC