How to resolve the algorithm Rot-13 step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rot-13 step by step in the BASIC programming language

Table of Contents

Problem Statement

Implement a   rot-13   function   (or procedure, class, subroutine, or other "callable" object as appropriate to your programming environment). Optionally wrap this function in a utility program   (like tr,   which acts like a common UNIX utility, performing a line-by-line rot-13 encoding of every line of input contained in each file listed on its command line,   or (if no filenames are passed thereon) acting as a filter on its   "standard input."

(A number of UNIX scripting languages and utilities, such as   awk   and   sed   either default to processing files in this way or have command line switches or modules to easily implement these wrapper semantics, e.g.,   Perl   and   Python). The   rot-13   encoding is commonly known from the early days of Usenet "Netnews" as a way of obfuscating text to prevent casual reading of   spoiler   or potentially offensive material. Many news reader and mail user agent programs have built-in rot-13 encoder/decoders or have the ability to feed a message through any external utility script for performing this (or other) actions. The definition of the rot-13 function is to simply replace every letter of the ASCII alphabet with the letter which is "rotated" 13 characters "around" the 26 letter alphabet from its normal cardinal position   (wrapping around from   z   to   a   as necessary). Thus the letters   abc   become   nop   and so on. Technically rot-13 is a   "mono-alphabetic substitution cipher"   with a trivial   "key". A proper implementation should work on upper and lower case letters, preserve case, and pass all non-alphabetic characters in the input stream through without alteration.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rot-13 step by step in the BASIC programming language

Source code in the basic programming language

CLS
INPUT "Enter a string: ", s$
ans$ = ""
FOR a = 1 TO LEN(s$)
        letter$ = MID$(s$, a, 1)
        IF letter$ >= "A" AND letter$ <= "Z" THEN
                char$ = CHR$(ASC(letter$) + 13)
                IF char$ > "Z" THEN char$ = CHR$(ASC(char$) - 26)
        ELSEIF letter$ >= "a" AND letter$ <= "z" THEN
                char$ = CHR$(ASC(letter$) + 13)
                IF char$ > "z" THEN char$ = CHR$(ASC(char$) - 26)
        ELSE
                char$ = letter$
        END IF
        ans$ = ans$ + char$
NEXT a
PRINT ans$


INPUT "Enter a string "; Text$
FOR c% = 1 TO LEN(Text$)
    SELECT CASE ASC(MID$(Text$, c%, 1))
        CASE 65 TO 90
            MID$(Text$, c%, 1) = CHR$(65 + ((ASC(MID$(Text$, c%, 1)) - 65 + 13) MOD 26))
        CASE 97 TO 122
            MID$(Text$, c%, 1) = CHR$(97 + ((ASC(MID$(Text$, c%, 1)) - 97 + 13) MOD 26))
    END SELECT
NEXT c%
PRINT "Converted......: "; Text$


100 rem ROT-13
110 cls : rem  110 HOME for Applesoft BASIC
120 input "Enter a string: ";a$
130 gosub 160
140 print b$
150 end
160 rem FUNCTION rot13
170 for i = 1 to len(a$)
180 n = asc(mid$(a$,i,1))
190 e = 255
200 if n > 64 and n < 91 then e = 90 : rem uppercase
210 if n > 96 and n < 123 then e = 122 : rem lowercase
220 if e < 255 then n = n+13
230 if n > e then n = n-26
240 b$ = b$+chr$(n)
250 next
260 return


100 REM ROT-13
110 CLS : REM  110 HOME for Applesoft BASIC
120 INPUT "Enter a string: ";a$
130 GOSUB 160
140 PRINT b$
150 END
160 REM FUNCTION rot13
170 FOR i = 1 TO LEN(a$)
180 n = ASC(MID$(a$,i,1))
190 e = 255
200 IF n > 64 AND n < 91 THEN e = 90 : REM uppercase
210 IF n > 96 AND n < 123 THEN e = 122 : REM lowercase
220 IF e < 255 THEN n = n+13
230 IF n > e THEN n = n-26
240 b$ = b$+CHR$(n)
250 NEXT
260 RETURN


FUNCTION rot13$(s$)
    LET ans$ = ""
    FOR a = 1 to len(s$)
        LET letter$ = (s$)[a:a+1-1]
        IF letter$ >= "A" and letter$ <= "Z" then
           LET char$ = chr$(ord(letter$[1:1])+13)
           IF char$ > "Z" then LET char$ = chr$(ord(char$[1:1])-26)
        ELSEIF letter$ >= "a" and letter$ <= "z" then
           LET char$ = chr$(ord(letter$[1:1])+13)
           IF char$ > "z" then LET char$ = chr$(ord(char$[1:1])-26)
        ELSE
           LET char$ = letter$
        END IF
        LET ans$ = ans$ & char$
    NEXT a
    LET s$ = ans$
    LET rot13$ = s$
END FUNCTION

INPUT prompt "Enter a string: ": s$
PRINT "Before encoding : "; s$
PRINT "After encoding  : "; rot13$(s$)
LET s$ = rot13$(s$)
PRINT "After decoding  : "; rot13$(s$)
END


PROGRAM  "Rot-13"
VERSION  "0.0000"

DECLARE FUNCTION  Entry ()

FUNCTION  Entry ()
	dec$ = ""
	TYPE$ = "cleartext "
	ctext$ = ""
	iOffset = 13    'offset assumed to be 13 - uncomment line 11 to change

	PRINT "For decrypt enter " + "<d> " + " -- else press enter > ";
	dec$ = INLINE$("")
	PRINT

	IF LCASE$(dec$) = "d" THEN
		iOffset = 26 - iOffset
		TYPE$ = "ciphertext "
	END IF

	PRINT "Enter " + TYPE$; '' + "> ";
	cad$ = UCASE$(INLINE$("> "))

	FOR i = 1 TO LEN(cad$)
		iTemp =  ASC(MID$(cad$,i,1))
		IF iTemp > 64 AND iTemp < 91 THEN
			iTemp = ((iTemp - 65) + iOffset) MOD 26
			PRINT CHR$(iTemp + 65);
		END IF
	NEXT i
END FUNCTION
END PROGRAM


  

You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Bait programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Python programming language
You may also check:How to resolve the algorithm Events step by step in the F# programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the C# programming language