Skip to content

Commit

Permalink
Create shift method
Browse files Browse the repository at this point in the history
  • Loading branch information
canro91 committed Apr 28, 2020
1 parent dafd111 commit 9d0573a
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions Day25/caesar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,15 @@ func main() {
}

func encrypt(k int, plainText string) {
for _, c := range plainText {
if !unicode.IsLetter(c) {
fmt.Print(string(c))
continue
}

var factor int
if unicode.IsUpper(c) {
factor = 'A'
} else {
factor = 'a'
}

e := (((int(c) + k) - factor) % 26) + factor
fmt.Print(string(e))
}
fmt.Println()
shift(plainText, k)
}

func decrypt(k int, cipherText string) {
for _, c := range cipherText {
shift(cipherText, -k)
}

func shift(text string, shift int) {
for _, c := range text {
if !unicode.IsLetter(c) {
fmt.Print(string(c))
continue
Expand All @@ -85,7 +73,7 @@ func decrypt(k int, cipherText string) {
factor = 'a'
}

e := (((int(c) - k) - factor) % 26) + factor
e := (((int(c) + shift) - factor) % 26) + factor
fmt.Print(string(e))
}
fmt.Println()
Expand Down

0 comments on commit 9d0573a

Please sign in to comment.