Comment detail

文字変換表に基く文字列の変換 (Nested Flatten)
(1)基本版です。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Tr("qwertyuiop", "QWERTYUIOP", "typewriter"));
    }

    static string Tr(string from, string to, string target)
    {
        char[] result = new char[target.Length];
        Dictionary<char, char> table = new Dictionary<char, char>();

        for (int i = 0; i < from.Length && i < to.Length; i++)
            table[from[i]] = to[i];

        for (int i = 0; i < target.Length; i++) {
            char c = target[i];
            result[i] = (table.ContainsKey(c)) ? table[c] : c;
        }

        return new string(result);
    }
}

Index

Feed

Other

Link

Pathtraq

loading...