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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rot-13 step by step in the Limbo 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 Limbo programming language

Source code in the limbo programming language

implement Rot13;

include "sys.m"; sys: Sys;
include "draw.m";

Rot13: module
{
	init:	fn(ctxt: ref Draw->Context, argv: list of string);
};

stdout: ref Sys->FD;
tab: array of int;

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;
	stdout = sys->fildes(1);
	inittab();
	args = tl args;
	if(args == nil)
		args = "-" :: nil;
	for(; args != nil; args = tl args){
		file := hd args;
		if(file != "-"){
			fd := sys->open(file, Sys->OREAD);
			if(fd == nil){
				sys->fprint(sys->fildes(2), "rot13: cannot open %s: %r\n", file);
				raise "fail:bad open";
			}
			rot13cat(fd, file);
		}else
			rot13cat(sys->fildes(0), "");
	}
}

inittab()
{
	tab = array[256] of int;
	for(i := 0; i < 256; i++)
		tab[i] = i;

	for(i = 'a'; i <= 'z'; i++)
		tab[i] = (((i - 'a') + 13) % 26) + 'a';
	for(i = 'A'; i <= 'Z'; i++)
		tab[i] = (((i - 'A') + 13) % 26) + 'A';
}


rot13(s: string): string
{
	for(i := 0; i < len s; i++) {
		if(s[i] < 256)
			s[i] = tab[s[i]];
	}
	return s;
}

rot13cat(fd: ref Sys->FD, file: string)
{
	buf := array[Sys->ATOMICIO] of byte;

	while((n := sys->read(fd, buf, len buf)) > 0) {
		obuf := array of byte (rot13(string buf));
		if(sys->write(stdout, obuf, n) < n) {
			sys->fprint(sys->fildes(2), "rot13: write error: %r\n");
			raise "fail:write error";
		}
	}
	if(n < 0) {
		sys->fprint(sys->fildes(2), "rot13: error reading %s: %r\n", file);
		raise "fail:read error";
	}
}


  

You may also check:How to resolve the algorithm Bitwise operations step by step in the 8051 Assembly programming language
You may also check:How to resolve the algorithm Bitmap/Read an image through a pipe step by step in the Phix programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the Scheme programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the zkl programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the REXX programming language