Caesar cipher: Difference between revisions
→{{header|REXX}}: Refurbished (variable names)
VincentARM (talk | contribs) m (correction error add) |
Walterpachl (talk | contribs) (→{{header|REXX}}: Refurbished (variable names)) |
||
Line 5,811:
===only Latin letters===
This version conforms to the task's restrictions.
<syntaxhighlight lang="rexx">/*REXX program supports the
/*
Say 'Caesar cipher key:' key /* echo the
code=caesar(text,key)
y= Caesar(p, key); say ' cyphered:' y /* " " cyphered text " " */▼
back=caesar(code,-key)
if z\==p then say "plain text doesn't match uncyphered cyphered text."▼
If back\==text Then
Say "unciphered text doesn't match plain text."
Caesar: procedure; arg s,k; @= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'▼
Exit
/*----------------------------------------------------------------------*/
L= length(@) /*obtain the length of the @ string. */▼
caesar: Procedure
Parse Arg text,key
_= verify(s, @) /*any illegal characters specified ? */▼
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. */▼
If badpos\==0 Then
Call err 'unsupported character:' substr(text,badpos,1)
If key>0 Then /* cipher */
key2=key+1
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= when using the input of: <tt> 22 The definition of a trivial program is one that has no bugs </tt>}}
<pre>
Line 5,844 ⟶ 5,857:
This version allows upper and lowercase Latin alphabet as well as all the
characters on the standard (computer) keyboard including blanks.
<syntaxhighlight lang="rexx">/*REXX program supports the Caesar
Parse Arg key text
Say 'Caesar cipher
code=caesar(text,key)
z= Caesar(y,-key); say ' uncyphered:' z /* " " uncyphered text " " */▼
back=caesar(code,-key)
exit 0 /*stick a fork in it, we're all done. */▼
If back\==text Then
Caesar: procedure; parse arg s,k; @= 'abcdefghijklmnopqrstuvwxyz'▼
@= translate(@)@"0123456789(){}[]<>'" /*add uppercase, digits, group symbols.*/▼
Exit
/*----------------------------------------------------------------------*/
▲ L= length(@) /*obtain the length of the @ string. */
caesar: Procedure
Parse Arg txt,ky
▲ if _\==0 then call err 'unsupported character:' substr(s, _, 1)
abcx=abcx'~!@#$%^&*_+:";?,./`-= ' /* also add other characters */
l=length(abcx)
aky=abs(ky)
If aky>length(abcx)-1|ky==0 Then
Call err ky 'key is invalid.'
If badpos\==0 Then
ky=ky+1
ky=l+1-aky
Return translate(txt,substr(abcx||abcx,ky,l),abcx)
/*----------------------------------------------------------------------*/
err:
Say
Say '***error***'
Say
Say arg(1)
Say
Exit 13</syntaxhighlight>
{{out|output|text= when using the input of: <tt> 31 Batman's hood is called a "cowl" (old meaning). </tt>}}
<pre>
Line 5,873 ⟶ 5,903:
uncyphered: Batman's hood is called a "cowl" (old meaning).
</pre>
=={{header|Ring}}==
<syntaxhighlight lang="ring">
|