Caesar cipher: Difference between revisions

Content added Content deleted
m (correction error add)
(→‎{{header|REXX}}: Refurbished (variable names))
Line 5,811: Line 5,811:
===only Latin letters===
===only Latin letters===
This version conforms to the task's restrictions.
This version conforms to the task's restrictions.
<syntaxhighlight lang="rexx">/*REXX program supports the Caesar cypher for the Latin alphabet only, no punctuation */
<syntaxhighlight lang="rexx">/*REXX program supports the Caesar cipher for the Latin alphabet only, */
/*──────────── or blanks allowed, all lowercase Latin letters are treated as uppercase.*/
/* no punctuation or blanks allowed, lowercase is treated as uppercase. */
parse arg key .; arg . p /*get key & uppercased text to be used.*/
Parse Upper Arg key text /* get key & uppercased text to be ciphered*/
p= space(p, 0) /*elide any and all spaces (blanks). */
text=space(text,0) /* elide any and blanks */
say 'Caesar cypher key:' key /*echo the Caesar cypher key to console*/
Say 'Caesar cipher key:' key /* echo the Caesar cipher key */
say ' plain text:' p /* " " plain text " " */
Say ' plain text:' text /* " " plain text */
code=caesar(text,key)
y= Caesar(p, key); say ' cyphered:' y /* " " cyphered text " " */
z= Caesar(y,-key); say ' uncyphered:' z /* " " uncyphered text " " */
Say ' ciphered:' code /* " " ciphered text */
back=caesar(code,-key)
if z\==p then say "plain text doesn't match uncyphered cyphered text."
exit 0 /*stick a fork in it, we're all done. */
Say ' unciphered:' back /* " " unciphered text */
If back\==text Then
/*──────────────────────────────────────────────────────────────────────────────────────*/
Say "unciphered text doesn't match plain text."
Caesar: procedure; arg s,k; @= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ak= abs(k) /*obtain the absolute value of the key.*/
Exit /* stick a fork in it, we're all done*/
/*----------------------------------------------------------------------*/
L= length(@) /*obtain the length of the @ string. */
caesar: Procedure
if ak>length(@)-1 | k==0 then call err k 'key is invalid.'
Parse Arg text,key
_= verify(s, @) /*any illegal characters specified ? */
abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
if _\==0 then call err 'unsupported character:' substr(s, _, 1)
If abs(key)>length(abc)-1|key==0 Then
if k>0 then ky= k + 1 /*either cypher it, or ··· */
Call err 'key ('key') is invalid.'
else ky= L + 1 - ak /* decypher it. */
return translate(s, substr(@ || @, ky, L), @) /*return the processed text.*/
badpos=verify(text,abc) /* any illegal character in the text */
If badpos\==0 Then
/*──────────────────────────────────────────────────────────────────────────────────────*/
Call err 'unsupported character:' substr(text,badpos,1)
err: say; say '***error***'; say; say arg(1); say; exit 13</syntaxhighlight>
If key>0 Then /* cipher */
key2=key+1
Else /* decipher */
key2=length(abc)+key+1
Return translate(text,substr(abc||abc,key2,length(abc)),abc)
/*----------------------------------------------------------------------*/
err:
Say
Say '***error***'
Say
Say arg(1)
Say
Exit 13</syntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 22 The definition of a trivial program is one that has no bugs </tt>}}
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 22 The definition of a trivial program is one that has no bugs </tt>}}
<pre>
<pre>
Line 5,844: Line 5,857:
This version allows upper and lowercase Latin alphabet as well as all the
This version allows upper and lowercase Latin alphabet as well as all the
characters on the standard (computer) keyboard including blanks.
characters on the standard (computer) keyboard including blanks.
<syntaxhighlight lang="rexx">/*REXX program supports the Caesar cypher for most keyboard characters including blanks.*/
<syntaxhighlight lang="rexx">/*REXX program supports the Caesar cipher for most keyboard characters */
parse arg key p /*get key and the text to be cyphered. */
/* including blanks.*/
say 'Caesar cypher key:' key /*echo the Caesar cypher key to console*/
Parse Arg key text /* get key and the text to be ciph */
say ' plain text:' p /* " " plain text " " */
Say 'Caesar cipher key:' key /* echo the Caesar cipher key */
y= Caesar(p, key); say ' cyphered:' y /* " " cyphered text " " */
Say ' plain text:' text /* " " plain text */
code=caesar(text,key)
z= Caesar(y,-key); say ' uncyphered:' z /* " " uncyphered text " " */
Say ' ciphered:' code /* " " ciphered text */
if z\==p then say "plain text doesn't match uncyphered cyphered text."
back=caesar(code,-key)
exit 0 /*stick a fork in it, we're all done. */
Say ' unciphered:' back /* " " unciphered text */
/*──────────────────────────────────────────────────────────────────────────────────────*/
If back\==text Then
Caesar: procedure; parse arg s,k; @= 'abcdefghijklmnopqrstuvwxyz'
Say "plain text doesn't match unciphered ciphered text."
@= translate(@)@"0123456789(){}[]<>'" /*add uppercase, digits, group symbols.*/
@= @'~!@#$%^&*_+:";?,./`-= ' /*also add other characters to the list*/
Exit /* stick a fork in it, we're all done */
/*----------------------------------------------------------------------*/
L= length(@) /*obtain the length of the @ string. */
caesar: Procedure
ak= abs(k) /*obtain the absolute value of the key.*/
Parse Arg txt,ky
if ak>length(@)-1 | k==0 then call err k 'key is invalid.'
abcx='abcdefghijklmnopqrstuvwxyz'
_= verify(s,@) /*any illegal characters specified ? */
abcx=translate(abcx)abcx"0123456789(){}[]<>'" /*add uppercase, digits */
if _\==0 then call err 'unsupported character:' substr(s, _, 1)
abcx=abcx'~!@#$%^&*_+:";?,./`-= ' /* also add other characters */
if k>0 then ky= k + 1 /*either cypher it, or ··· */
else ky= L + 1 - ak /* decypher it. */
l=length(abcx) /* obtain the length of abcx */
return translate(s, substr(@ || @, ky, L), @) /*return the processed text.*/
aky=abs(ky) /* absolute value of the key */
If aky>length(abcx)-1|ky==0 Then
/*──────────────────────────────────────────────────────────────────────────────────────*/
Call err ky 'key is invalid.'
err: say; say '***error***'; say; say arg(1); say; exit 13</syntaxhighlight>
badpos=verify(txt,abcx) /* any illegal character in txt */
If badpos\==0 Then
Call err 'unsupported character:' substr(txt,badpos,1)
If ky>0 Then /* cipher */
ky=ky+1
Else /* decipher */
ky=l+1-aky
/* return translated input */
Return translate(txt,substr(abcx||abcx,ky,l),abcx)
/*----------------------------------------------------------------------*/
err:
Say
Say '***error***'
Say
Say arg(1)
Say
Exit 13</syntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 31 Batman's hood is called a "cowl" (old meaning). </tt>}}
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 31 Batman's hood is called a "cowl" (old meaning). </tt>}}
<pre>
<pre>
Line 5,873: Line 5,903:
uncyphered: Batman's hood is called a "cowl" (old meaning).
uncyphered: Batman's hood is called a "cowl" (old meaning).
</pre>
</pre>

=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang="ring">
<syntaxhighlight lang="ring">