S rot13(S)(S s) if (isSomeString!S) {
return rot(s, 13);
}

S rot(S)(S s, int key) if (isSomeString!S) {
auto r = new dchar[s.walkLength()];

foreach (i, dchar c; s) {
if (isLower(c))
r[i] = ((c - 'a' + key) % 26 + 'a');
else if (isUpper(c))
r[i] = ((c - 'A' + key) % 26 + 'A');
}
return to!S(r);
}

this is better

S rot13(S)(in S s) if (isSomeString!S) {
    return rot(s, 13);
}

S rot(S)(in S s, in int key) if (isSomeString!S) {
    auto r = new dchar[s.walkLength];

    foreach (i, dchar c; s) {
        if (std.ascii.isLower(c))
            c = ((c - 'a' + key) % 26 + 'a');
        else if (std.ascii.isUpper(c))
            c = ((c - 'A' + key) % 26 + 'A');
        r[i] = c;
    }
    return to!S(r);
}

Reply via email to