Help us understand the problem. What is going on with this article?

08. 暗号文

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"));

まとめ

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

トップ

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away