Caesar cipher: Difference between revisions
Content added Content deleted
VincentARM (talk | contribs) m (correction error add) |
Walterpachl (talk | contribs) (→{{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 |
<syntaxhighlight lang="rexx">/*REXX program supports the Caesar cipher for the Latin alphabet only, */ |
||
/* |
/* no punctuation or blanks allowed, lowercase is treated as uppercase. */ |
||
Parse Upper Arg key text /* get key & uppercased text to be ciphered*/ |
|||
text=space(text,0) /* elide any and blanks */ |
|||
Say 'Caesar cipher key:' key /* echo the Caesar cipher key */ |
|||
Say ' plain text:' text /* " " plain text */ |
|||
code=caesar(text,key) |
|||
⚫ | |||
Say ' ciphered:' code /* " " ciphered text */ |
|||
back=caesar(code,-key) |
|||
⚫ | |||
Say ' unciphered:' back /* " " unciphered text */ |
|||
If back\==text Then |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
Say "unciphered text doesn't match plain text." |
|||
⚫ | |||
Exit /* stick a fork in it, we're all done*/ |
|||
/*----------------------------------------------------------------------*/ |
|||
⚫ | |||
caesar: Procedure |
|||
if ak>length(@)-1 | k==0 then call err k 'key is invalid.' |
|||
Parse Arg text,key |
|||
⚫ | |||
⚫ | |||
⚫ | |||
If abs(key)>length(abc)-1|key==0 Then |
|||
⚫ | |||
Call err 'key ('key') is invalid.' |
|||
⚫ | |||
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 |
|||
⚫ | |||
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>}} |
{{out|output|text= when using the input of: <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 |
<syntaxhighlight lang="rexx">/*REXX program supports the Caesar cipher for most keyboard characters */ |
||
/* including blanks.*/ |
|||
Parse Arg key text /* get key and the text to be ciph */ |
|||
Say 'Caesar cipher key:' key /* echo the Caesar cipher key */ |
|||
Say ' plain text:' text /* " " plain text */ |
|||
code=caesar(text,key) |
|||
⚫ | |||
⚫ | |||
if z\==p then say "plain text doesn't match uncyphered cyphered text." |
|||
back=caesar(code,-key) |
|||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
If back\==text Then |
|||
⚫ | |||
⚫ | |||
⚫ | |||
Exit /* stick a fork in it, we're all done */ |
|||
/*----------------------------------------------------------------------*/ |
|||
⚫ | |||
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.' |
|||
⚫ | |||
_= verify(s,@) /*any illegal characters specified ? */ |
|||
⚫ | |||
⚫ | |||
abcx=abcx'~!@#$%^&*_+:";?,./`-= ' /* also add other characters */ |
|||
if k>0 then ky= k + 1 /*either cypher it, or ··· */ |
|||
l=length(abcx) /* obtain the length of abcx */ |
|||
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> |
|||
⚫ | |||
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>}} |
{{out|output|text= when using the input of: <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"> |