Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
More than 1 year has 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"));

まとめ

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

トップ

0
Help us understand the problem. What is going on with this article?
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

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?