Skip to content

Instantly share code, notes, and snippets.

@edwardmp
Last active March 13, 2024 20:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edwardmp/3aca97114eb19089e18d to your computer and use it in GitHub Desktop.
Save edwardmp/3aca97114eb19089e18d to your computer and use it in GitHub Desktop.
A Tour of Go, exercise rot13Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (rot *rot13Reader) Read(p []byte) (n int, err error) {
n, err = rot.r.Read(p)
for i := 0; i < len(p); i++ {
if p[i] >= 'A' && p[i] < 'Z' {
p[i] = 65 + (((p[i] - 65) + 13) % 26)
} else if p[i] >= 'a' && p[i] <= 'z' {
p[i] = 97 + (((p[i] - 97) + 13) % 26)
}
}
return
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
@vrlmv
Copy link

vrlmv commented Sep 27, 2020

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r rot13Reader) Read(b []byte) (int, error) {
	n, err := r.r.Read(b)
	if err == nil {
		for i := range b {
			switch true {
			case b[i] >= 97 && b[i] <= 109:
				b[i] += 13
			case b[i] > 109 && b[i] <= 122:
				b[i] -= 13
			case b[i] >= 65 && b[i] <= 77:
				b[i] += 13
			case b[i] > 77 && b[i] <= 90:
				b[i] -= 13
			}
		}
	}
	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@mo-xiaoming
Copy link

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r rot13Reader) Read(b []byte) (int, error) {
	var table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM"
	n, err := r.r.Read(b)
	if err == nil {
		for i := 0; i < n; i++ {
			var c = b[i]
			if 'A' <= c && c <= 'Z' {
				b[i] = table[c-'A'+13]
			} else if 'a' <= c && c <= 'z' {
				b[i] = table[c-'a'+39]
			}
		}
	}
	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@ilessy
Copy link

ilessy commented Dec 19, 2020

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r *rot13Reader) Read(p []byte) (n int, err error) {
	n, err = r.r.Read(p)
	for i := 0; i < n; i++ {
		if p[i] >= 'A' && p[i] <= 'a' || p[i] >= 'a' && p[i] <= 'z' {
			p[i] += 13
			if p[i] > 'z' {
				p[i] -= 26
			}
		}
	}
	return
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@ranvit
Copy link

ranvit commented Jan 6, 2021

Solution using switch cases!

func (r rot13Reader) Read(b []byte) (int, error) {
	n, e := r.r.Read(b)
	if e != nil {
		return 0, e
	}
	for i := range b {
		switch {
		case b[i] >= 'A' && b[i] <= 'Z':
			b[i] = ((b[i] - 'A' + 13) % 26) + 'A'
		case b[i] >= 'a' && b[i] <= 'z':
			b[i] = ((b[i] - 'a' + 13) % 26) + 'a'
		default:
		}

	}
	return n, nil
}

@sschwarzer
Copy link

sschwarzer commented Jan 26, 2021

func (r rot13Reader) Read(b []byte) (int, error) {
	n, err := r.r.Read(b)
	if err != nil {
		return 0, err
	}
	// Not sure if a range over `b` is always correct.
	// Can't the slice have higher length than n?
	for i := 0; i < n; i++ {
		c := b[i]
		if ('A' <= c && c <= 'M') || ('a' <= c && c <= 'm') {
			b[i] += 13
		} else if ('N' <= c && c <= 'Z') || ('n' <= c && c <= 'z') {
			b[i] -= 13
		}
	}
	return n, nil
}

@abheyogy
Copy link

abheyogy commented Mar 1, 2021

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r rot13Reader) Read(b []byte) (int, error) {
	n, err := r.r.Read(b)
	if err == nil {
		for i := range b {
			switch true {
			case b[i] >= 97 && b[i] <= 109:
				b[i] += 13
			case b[i] > 109 && b[i] <= 122:
				b[i] -= 13
			case b[i] >= 65 && b[i] <= 77:
				b[i] += 13
			case b[i] > 77 && b[i] <= 90:
				b[i] -= 13
			}
		}
	}
	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

Do not need the true at the switch statement. By default nothing means true. Also kudos, this version is easy to read!

@Nishith-Savla
Copy link

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot *rot13Reader) Read(b []byte) (int, error) {
	n, err := rot.r.Read(b)
	if err == nil {
		firstLetter := byte(0)
		for i := 0; i < len(b); i++ {
			if b[i] >= 'A' && b[i] <= 'Z' {
				firstLetter = 'A'
			} else if b[i] >= 'a' && b[i] <= 'z' {
				firstLetter = 'a'
			} else {
				continue
			}
			b[i] = firstLetter + ((b[i] - firstLetter + 13) % 26)
		}
	}
	return n, err	
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

My approach to increase readability and avoid code duplication

@miemiekurisu
Copy link

package main

import (
	"io"
	"os"
	"strings"
)

const orig string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
const rot13table string = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm "

type rot13Reader struct {
	r io.Reader
}

func buildMap() map[rune]rune {
	var dict map[rune]rune
	dict = make(map[rune]rune)
	origRune := []rune(orig)

	tableRune := []rune(rot13table)
	for idx, char := range origRune {
		dict[char] = tableRune[idx]
	}
	//fmt.Println(dict)
	return dict
}

func (rot *rot13Reader) Read(b []byte) (n int, err error) {

	n, err = rot.r.Read(b)
	dict := buildMap()
	for i := 0; i < len(b); i++ {
		r2, contains := dict[rune(b[i])]
		if contains {
			b[i] = byte(r2)
		} else {
			continue
		}

	}

	return n, err

}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@rahulmaindargi
Copy link

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r rot13Reader) Read(b []byte) (int,error) {
	res, err :=  r.r.Read(b)
	for i:=0;i<len(b);i++ {
		if b[i] >= 'a' && b[i] <= 'z' {
			b[i]-=13
			if(b[i]< 'a') {
				b[i]+=26
			}
		}
		if( b[i] >= 'A' && b[i] <= 'Z') {
			b[i]-=13
			if(b[i]< 'A') {
				b[i]+=26
			}
		}
	}
	return res, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@egor-masterskikh
Copy link

egor-masterskikh commented Jul 27, 2021

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r *rot13Reader) Read(p []byte) (n int, err error) {
	n, err = r.r.Read(p)
	for i, code := range p {
		if 'A' <= code && code <= 'M' || 'a' <= code && code <= 'm' {
			p[i] += 13
		} else if 'N' <= code && code <= 'Z' || 'n' <= code && code <= 'z' {
			p[i] -= 13
		}
	}
	return
}


func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@Karllzy
Copy link

Karllzy commented Jul 29, 2021

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (self rot13Reader) Read(b []byte) (int, error) {
	num, err := self.r.Read(b)
	if err == nil {
		for i := 0; i < num; i++ {
			switch {
			case 'A' < b[i] && b[i] < 'Z':
				b[i] = 'A' + ((b[i] - 'A' + 13) % 26)
			case 'a' < b[i] && b[i] < 'z':
				b[i] = 'a' + ((b[i] - 'a' + 13) % 26)
			}
		}
	}
	return num, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@Zakaria-097
Copy link

Zakaria-097 commented Jul 29, 2021

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot *rot13Reader) Read(b[]byte) (n int, err error){

	n, err = rot.r.Read(b)
	
	for i := 0; i < len(b); i++{
		if b[i] >= 66 && b[i] <= 123 {
			b[i] = b[i] + 13
			if b[i] >= 123{
				b[i] = b[i] - 26
			}
		}
	}
	return
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@Shohei-Yamamoto
Copy link

Shohei-Yamamoto commented Aug 20, 2021

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot rot13Reader) Read(p []byte) (l int, err error) {
	if l, err = rot.r.Read(p); err != nil { return }
	for i:=0; i<l; i+=1{
		p[i] = convertToRot(13, p[i])
	}
	return 
}

func convertToRot(rot byte, b byte) byte {
	switch {
		case b <= 'Z' && 'Z' - rot + 1 <= b: 
			return (b + rot) % ('Z' + 1) + 'A'
		case b <= 'z' && 'z' - rot + 1 <= b:
			return (b + rot) % ('z' + 1) + 'a'
		default: 	
			return b + rot
	}
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@mike-kirpa
Copy link

mike-kirpa commented Aug 25, 2021

`package main

import (
"io"
"os"
"strings"
)

type rot13Reader struct {
r io.Reader
}

func (rot *rot13Reader) Read(b []byte) (int, error) {
n, err := rot.r.Read(b)
if err != nil {
return 0, err
}
for i := 0; i < len(b); i++ {
if b[i] >= 'A' && b[i] < 'M' || b[i] >= 'a' && b[i] <= 'm' {
b[i] += 13
} else if b[i] >= 'N' && b[i] < 'Z' || b[i] >= 'n' && b[i] <= 'z' {
b[i] -= 13
}
}
return n, nil
}

func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}`

@DrH97
Copy link

DrH97 commented Nov 18, 2021

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot rot13Reader) Read(r []byte) (int, error) {
	
	n, err := rot.r.Read(r)	
	
	for j := range r {

		if r[j] >= 'A' && r[j] <= 'z' {
			if (r[j] > 'M' && r[j] <= 'Z') || (r[j] > 'm') {
				r[j] -= 13
			} else {
				r[j] += 13
			}
		}
		
	}	
		
	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@vadim-sychov
Copy link

package main

import (
	"io"
	"os"
	"strings"
)

const minASCIITableLatinCode = 64
const maxASCIITableLatinCode = 122
const rot13CipherOffset = 13

type rot13Reader struct {
	r io.Reader
}

func (rot13 *rot13Reader) Read(buffer []byte) (n int, err error) {
	n, err = rot13.r.Read(buffer)

	if err != nil {
		return 0, err
	}

	for key, _ := range buffer {
		translatedUtf8CharCode := buffer[key] + rot13CipherOffset

		if translatedUtf8CharCode > maxASCIITableLatinCode {
			translatedUtf8CharCode = translatedUtf8CharCode - maxASCIITableLatinCode + minASCIITableLatinCode
		}

		buffer[key] = translatedUtf8CharCode
	}

	return
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}

	io.Copy(os.Stdout, &r)
}

@Cacklingallnight
Copy link

Not sure if it's correct, but at least I tried.

package main

import (
	"io"
	"os"
	"strings"
)

const input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"

type Rot13Reader struct {
	r io.Reader
}

func (a Rot13Reader) Read(b []byte) (int, error) {
	l, err := a.r.Read(b)

	if err != nil {
		return 0, err
	}
	for i := 0; i < l; i++ {
		idx := strings.IndexByte(input, b[i])
		if idx != -1 {
			b[i] = byte(output[idx])
		}

	}

	return l, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := Rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@bvpav
Copy link

bvpav commented Apr 12, 2022

A simple check would involve:

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot rot13Reader) Read(p []byte) (int, error) {
	n, err := rot.r.Read(p)
	for i := 0; i < n; i++ {
		letterNum := p[i] | 32 - 'a'
		switch {
		case letterNum < 13:
			p[i] = p[i] + 13
		case 13 <= letterNum && letterNum < 26:
			p[i] = p[i] - 13
		}
	}
	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

Or in case you prefer a map lookup

var rot13table = map[byte]byte{
	'A': 'N',
	'B': 'O',
	'C': 'P',
	'D': 'Q',
	'E': 'R',
	'F': 'S',
	'G': 'T',
	'H': 'U',
	'I': 'V',
	'J': 'W',
	'K': 'X',
	'L': 'Y',
	'M': 'Z',
	'N': 'A',
	'O': 'B',
	'P': 'C',
	'Q': 'D',
	'R': 'E',
	'S': 'F',
	'T': 'G',
	'U': 'H',
	'V': 'I',
	'W': 'J',
	'X': 'K',
	'Y': 'L',
	'Z': 'M',
	'a': 'n',
	'b': 'o',
	'c': 'p',
	'd': 'q',
	'e': 'r',
	'f': 's',
	'g': 't',
	'h': 'u',
	'i': 'v',
	'j': 'w',
	'k': 'x',
	'l': 'y',
	'm': 'z',
	'n': 'a',
	'o': 'b',
	'p': 'c',
	'q': 'd',
	'r': 'e',
	's': 'f',
	't': 'g',
	'u': 'h',
	'v': 'i',
	'w': 'j',
	'x': 'k',
	'y': 'l',
	'z': 'm',
}

func (rot rot13Reader) Read(p []byte) (int, error) {
	n, err := rot.r.Read(p)
	for i := 0; i < n; i++ {
		if c, ok := rot13table[p[i]]; ok {
			p[i] = c
		}
	}
	return n, err
}

However that takes up more code real estate.

@unifreak
Copy link

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot rot13Reader) Read(b []byte) (int, error) {
	n, err := rot.r.Read(b)
	if err != nil {
		return n, err
	}
	for i, v := range b {
		if 'A' <= v && v <= 'Z' {
			mod := byte('Z' + 1)
			q := (v+13) / mod
			b[i] = q*'A' + (v + 13) % mod
		}

		if 'a' <= v && v <= 'z' {
			mod := byte('z' + 1)
			q := (v+13) / mod
			b[i] = q*'a' + (v + 13) % mod
		}
	}
	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@TimopheyKor
Copy link

It took a little bit, but I'm happy with my implementation of this one., and had fun trying to clean it up. Might have some unnecessary code. Commented with explanations.

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

// Read loops over a []byte b for the number of bytes populated (n).
// Each byte in b is replaced with its rot13 substitution cypher.
func (rot *rot13Reader) Read(b []byte) (n int, err error) {
	n, err = rot.r.Read(b)
	
	for i := 0; i < n; i++ {
		b[i] = rot13Byte(b[i])
		
		// Unexpected error handling; unsure if this would ever catch.
		if err != nil {
			break
		}
	}

	return
}

// rot13Byte takes a byte and returns its rot13 substitution cypher
// if the byte corresponds to an alphabetic character.
func rot13Byte(b byte) byte {
	
	// If the byte is capital or lowercase A - M, shift it 13 letters up.
	if b >= 'A' && b <= 'M' || b >= 'a' && b <= 'm' {
		return b + 13
	}
	
	// If the byte is capital or lowercase N - Z, shift it 13 letters down.
	if b >= 'N' && b <= 'Z' || b >= 'n' && b <= 'z' {
		return b - 13	
	}
	
	return b
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@vojtaripa
Copy link

package main

import (
    "io"
    "os"
    "strings"
    //"fmt"

)

//GIVEN INTERFACE FOR -> rot13Reader
type rot13Reader struct {
    r io.Reader
}

//MY READ METHOD: (needs to return (n int, err error))
func (passedR rot13Reader) Read(p []byte) (int, error){
    
    //FIRST GETTING VARIABLES FROM rot13Reader that was passed in.
    n, err := passedR.r.Read(p)
    
    //THEN LOOP length of p and get / change each letter (without the loop below the program works but stays the same!
    
    for i := 0; i < len(p); i++ {
	    
	    //DEPENDING ON INPUT, ADD OR SUBTRACT 13 ACCORDINGLY:
	    switch {
	    case(p[i]>='A' && p[i]<='M'):
		    p[i] += 13
        case(p[i]>='N' && p[i]<='Z'):
		    p[i] -= 13
	    case(p[i]>='a' && p[i]<='m'):
		    p[i] += 13
        case(p[i]>='n' && p[i]<='z'):
		    p[i] -= 13		
	    default:
		    p[i]=p[i]
        }
    }
    
    //FINALLY RETURN n and error
    return n, err
    
}

func main() {
    //MAKE A NEW STRING
    s := strings.NewReader("Lbh penpxrq gur pbqr!")
    
    //MAKE A NEW rot13Reader with string above
    r := rot13Reader{s}
    
    //now copy it?
    io.Copy(os.Stdout, &r)
}

OUTPUT:

You cracked the code!

@krapie
Copy link

krapie commented Dec 9, 2022

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func rot13(b byte) byte {
	var a, z byte
	
	switch {
		case 'a' <= b && b <= 'z':
			a, z = 'a', 'z'
		case 'A' <= b && b <= 'Z':
			a, z = 'A', 'Z'
		default:
			return b
	}
	
	return (b-a+13)%(z-a+1) + a
}

func (reader rot13Reader) Read(b []byte) (int, error) {
	n, err := reader.r.Read(b)
		
	for i := 0; i < n; i++ {
		b[i] = rot13(b[i])
	}
	
	return n, err
}	

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@happen2me
Copy link

Easier for understanding

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r rot13Reader) Read(b []byte) (int, error) {
	n, err := r.r.Read(b)
	for i := 0; i < n; i++ {
		switch {
		case b[i] >= byte('A') && b[i] < byte('N'):
			b[i] += 13
		case b[i] >= byte('a') && b[i] < byte('n'):
			b[i] += 13
		case b[i] >= byte('N') && b[i] <= byte('Z'):
			b[i] -= 13
		case b[i] >= byte('n') && b[i] <= byte('z'):
			b[i] -= 13
		}
	}
	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@curbol
Copy link

curbol commented Jun 9, 2023

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot *rot13Reader) Read(p []byte) (n int, err error) {
	n, err = rot.r.Read(p)
	if err != nil {
		return n, err
	}
	for i := range p {
		switch {
			case p[i] >= 'A' && p[i] <= 'M':
				p[i] += 13
			case p[i] >= 'N' && p[i] <= 'Z':
				p[i] -= 13
			case p[i] >= 'a' && p[i] <= 'm':
				p[i] += 13
			case p[i] >= 'n' && p[i] <= 'z':
				p[i] -= 13
		}
	}
	return
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@wildwind123
Copy link

wildwind123 commented Jul 1, 2023

package main

import (
	"io"
	"os"
	"strings"
)

const input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"

type rot13Reader struct {
	r io.Reader
}

func (r *rot13Reader) Read(b []byte) (int, error) {
	n, err := r.r.Read(b)
	if err != nil {
		return n, err
	}

	for i := range b {
		inputIndex := strings.Index(input, string(b[i]))
		if inputIndex < 0 {
			continue
		}
		b[i] = output[inputIndex]
	}

	return n, nil
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@Max95Cohen
Copy link

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r rot13Reader) Read(bytes []byte) (int, error) {
	n, err := r.r.Read(bytes)

	for i := 0; i < n; i++ {
		byte := bytes[i]

		if byte >= 'A' && byte <= 'M' {
			bytes[i] += 13
		}

		if byte >= 'N' && byte <= 'Z' {
			bytes[i] -= 13
		}

		if byte >= 'a' && byte <= 'm' {
			bytes[i] += 13
		}

		if byte >= 'n' && byte <= 'z' {
			bytes[i] -= 13
		}
	}

	return n, err
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}

	io.Copy(os.Stdout, &r)
}

@imtinanahmad-xpertdigi
Copy link

package main

import (
"io"
"os"
"strings"
)

type rot13Reader struct {
r io.Reader
}

func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, r)
}

func (rot rot13Reader) Read(b []byte) (int, error) {
v, err := rot.r.Read(b)

for i := 0; i < v; i++ {
	b[i] = rot13(string(b[i]))[0]
}

if err != nil {
	return 0, err
}

return v, nil

}

func rot13(s string) (string) {
s = strings.ToUpper(s)

for i := 0; i < len(s); i++ {

	if string(s[i]) < "M" {
		return string( s[i] + 13)
	}

	if string(s[i]) > "M" {
		return string( s[i] - 13)
	}
}

return s;

}

@rockswe
Copy link

rockswe commented Dec 29, 2023

package main

import (
"io"
"os"
"strings"
)

type rot13Reader struct {
r io.Reader
}

func (rot *rot13Reader) Read(p []byte) (n int, err error) {
n, err = rot.r.Read(p)
for i := 0; i < len(p); i++ {
if p[i] >= 'A' && p[i] < 'Z' {
p[i] = 65 + (((p[i] - 65) + 13) % 26)
} else if p[i] >= 'a' && p[i] <= 'z' {
p[i] = 97 + (((p[i] - 98) + 13) % 26)
}
}
return
}

func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}

@Atavixion
Copy link

Atavixion commented Jan 7, 2024

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (r *rot13Reader) Read(b []byte) (int, error) {
	if n, err := r.r.Read(b); err != nil {
		return n, err
	}
	
	for i, v := range b {
		if v > 64 && v < 91 {
			b[i] = (v - 65 + 13) % 26 + 65
		} else if v > 96 && v < 123 {
			b[i] = (v - 97 + 13) % 26 + 97
		}
	}
	return len(b), nil
}
	
func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

@PowerPixel
Copy link

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot13 rot13Reader) Read(out []byte) (int, error) {
	n, err := rot13.r.Read(out)
	if err != nil {
		return n, err	
	}
	
	for i := 0; i < len(out); i++ {
		if out[i] >= 'A' && out[i] <= 'Z' {
			out[i] = 'A' + (((out[i] - 'A') + 13) % 26)
		}
		if out[i] >= 'a' && out[i] <= 'z' {
			out[i] = 'a' + (((out[i] - 'a') + 13) % 26)
		}
	}
	return n, nil
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

I wanted to do compact & efficient :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment