2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

08. 暗号文

与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ.

英小文字ならば(219 - 文字コード)の文字に置換
その他の文字はそのまま出力
この関数を用い,英語のメッセージを暗号化・復号化せよ.

###Go

package main

import "fmt"

func cipher(s string) string {
	r  := ""
	for i := 0;i<len(s); i++ {
		if s[i] >= 'a' && s[i] <= 'z' {
			r += string(219 - s[i]);
		} else {
			r += string(s[i]);
		}
	}

	return r;
}

func main() {
	s := cipher("AaBcCcDd1234efgEFG");
	fmt.Println(s);
}

###python

# -*- coding: utf-8 -*-

def cipher(s):
    r = ""
    for i in range(len(s)):
        if s[i].islower():
            r += chr(219 - ord(s[i]))
        else:
            r += s[i]

    return r

print cipher("AaBcCcDd1234efgEFG")

###Javascript

function cipher(s) {
    r = "";
    for (var i = 0; i < s.length; i++) {
        if (s[i] >= 'a' && s[i] <= 'z') {
            r += String.fromCharCode(219 - s[i].charCodeAt(0));
        }
        else {
            r += s[i];
        }
    }

    return r;
}

console.log(cipher("AaBcCcDd1234efgEFG"));

まとめ

微妙に長いソースになった。もっとスッキリ書ける様にしたい。

トップ

2
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?