| | |
Help With Caesar Cipher/Shift
Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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...
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)
Program shift; VAR asciicode, number : Integer; letter, asciiletter, newcode : Char; Procedure input; BEGIN Writeln ('Enter a word'); readln(letter); end; Procedure convert; BEGIN asciicode := ord(letter); asciiletter := chr(number); newcode := chr(asciicode+2); end; Procedure output; BEGIN Writeln ('The ASCII code of ',letter,' is ',asciicode); Writeln (newcode); Readln; end; BEGIN input; convert; output; END.
There are 10 people in the world, those that understand binary and those that don't.
•
•
Join Date: Apr 2007
Posts: 27
Reputation:
Solved Threads: 1
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)
Also note that you can use the result of Ord and Chr without storing them in intermediate variables.
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)
procedure Convert(var s :string); var i :integer; begin for i := 1 to Length(s) do if Ord(s[i]) >= Ord('Y') then s[i] := Chr(Ord(s[i])-24) else s[i] := Chr(Ord(s[i])+2); end;
Also note that you can use the result of Ord and Chr without storing them in intermediate variables.
*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...
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.
•
•
Join Date: Apr 2007
Posts: 27
Reputation:
Solved Threads: 1
•
•
•
•
*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...
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.
•
•
Join Date: Nov 2007
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
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:
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.
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.
![]() |
Similar Threads
- Word Association Game (Posting Games)
- Need help with Caesar Cipher program (Java)
- Need help with caesar cipher (Python)
- Caesar cipher problem with CHR(32) (Visual Basic 4 / 5 / 6)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Web Enabling Applications
- Next Thread: help!!!
Views: 3988 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Pascal and Delphi






