Jump to content

Caesar cipher: Difference between revisions

1,152 bytes removed ,  10 months ago
→‎{{header|REXX}}: Refurbished (variable names)
m (correction error add)
(→‎{{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 Caesar cyphercipher for the Latin alphabet only, no punctuation */
/*──────────── no punctuation or blanks allowed, all lowercase Latin letters areis treated as uppercase. */
parseParse argUpper Arg key .; arg . p text /* get key & uppercased text to be used.ciphered*/
ptext= space(ptext, 0) /* elide any and blanks /*elide any and all spaces (blanks). */
Say 'Caesar cipher key:' key /* echo the say 'Caesar cyphercipher key:' key /*echo the Caesar cypher key to console */
saySay ' plain text:' ptext /* " " plain text " " */
code=caesar(text,key)
y= Caesar(p, key); say ' cyphered:' y /* " " cyphered text " " */
z=Say Caesar(y,-key);' say ' uncypheredciphered:' zcode /* " " uncyphered ciphered text " " */
back=caesar(code,-key)
if z\==p then say "plain text doesn't match uncyphered cyphered text."
exitSay 0' unciphered:' back /* " " unciphered text /*stick a fork in it, we're all done. */
If back\==text Then
/*──────────────────────────────────────────────────────────────────────────────────────*/
Say "unciphered text doesn't match plain text."
Caesar: procedure; arg s,k; @= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Exit ak= abs(k) /* stick a fork in it, we're all /*obtain the absolute value of the key.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 ? */
Caesar: procedure; arg s,k; @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 translatebadpos=verify(stext,abc) substr(@ || @, ky, L), @)/* any illegal /*returncharacter in the processed 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 if k>0 then ky= k + 1 /*either cypherdecipher it, or ··· */
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>}}
<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 cyphercipher for most keyboard characters including blanks.*/
parse/* arg key p /*get key and the text to be cyphered. including blanks.*/
Parse Arg key text say 'Caesar/* cypherget key:' key /*echoand the Caesartext cypherto keybe ciph to console*/
Say 'Caesar cipher say key:' key plain text:' p /* echo " " plain text the Caesar cipher "key " */
y= Caesar(p, key); saySay ' plain cypheredtext:' ytext /* " " cyphered plain text " " */
code=caesar(text,key)
z= Caesar(y,-key); say ' uncyphered:' z /* " " uncyphered text " " */
y= Caesar(p, key); saySay ' cypheredciphered:' ycode /* " " cypheredciphered 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. */
z= Caesar(y,-key); saySay ' uncypheredunciphered:' zback /* " " uncypheredunciphered text " " */
/*──────────────────────────────────────────────────────────────────────────────────────*/
If back\==text Then
Caesar: procedure; parse arg s,k; @= 'abcdefghijklmnopqrstuvwxyz'
if z\==p then say Say "plain text doesn't match uncypheredunciphered cypheredciphered text."
@= translate(@)@"0123456789(){}[]<>'" /*add uppercase, digits, group symbols.*/
Exit @= @'~!@#$%^&*_+:";?,./`-= ' /*also addstick othera charactersfork in it, we're toall thedone list*/
/*----------------------------------------------------------------------*/
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.'
Caesar: procedure; parse arg s,k; @abcx= 'abcdefghijklmnopqrstuvwxyz'
_= verify(s,@) /*any illegal characters specified ? */
@abcx= translate(@abcx)@abcx"0123456789(){}[]<>'" /*add uppercase, digits, group symbols.*/
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 ··· */
l=length(abcx) else ky= L + 1 - ak /* obtain the length of decypher it. abcx */
aky=abs(ky) return translate(s, substr(@ || @, ky, L), @) /*return absolute value of the processedkey text.*/
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(stxt, @abcx) /* any illegal characters specifiedcharacter ?in txt */
If badpos\==0 Then
if _\==0 then callCall err 'unsupported character:' substr(stxt, _badpos, 1)
exit If ky>0 Then /* cipher /*stick a fork in it, we're all done. */
ky=ky+1
Else else ky= L + 1 - ak /* decipher decypher it. */
ky=l+1-aky
L= length(@) /*obtain thereturn translated lengthinput of the @ string. */
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>}}
<pre>
Line 5,873 ⟶ 5,903:
uncyphered: Batman's hood is called a "cowl" (old meaning).
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
2,295

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.