Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

A suggestion was made to post this question on here, so here is a copy from my original question that I posted on Stack Overflow:

I would like to know how to reverse the individual characters in a string, without reversing the whole string literally. I used the code that is showed below and got the following results. I will also show what the result should be.

  • Input: a29z3
  • Output: a20a3
  • What it should be: z70a6

As you can see, I'm not getting the result that I expected in the beginning. Every individual character should be reversed, but I'm curious how to do this. The result above is what I got with the following code:

static void Main()
        string input = "a29z3";

        //Reverse Letters
        input = input.Replace("a", "z");
        input = input.Replace("b", "y");
        input = input.Replace("c", "x");
        input = input.Replace("d", "w");
        input = input.Replace("e", "v");
        input = input.Replace("f", "u");
        input = input.Replace("g", "t");
        input = input.Replace("h", "s");
        input = input.Replace("i", "r");
        input = input.Replace("j", "q");
        input = input.Replace("k", "p");
        input = input.Replace("l", "o");
        input = input.Replace("m", "n");
        input = input.Replace("n", "m");
        input = input.Replace("o", "l");
        input = input.Replace("p", "k");
        input = input.Replace("q", "j");
        input = input.Replace("r", "i");
        input = input.Replace("s", "h");
        input = input.Replace("t", "g");
        input = input.Replace("u", "f");
        input = input.Replace("v", "e");
        input = input.Replace("w", "d");
        input = input.Replace("x", "c");
        input = input.Replace("y", "b");
        input = input.Replace("z", "a");

        //Reverse numbers
        input = input.Replace("0", "9");
        input = input.Replace("1", "8");
        input = input.Replace("2", "7");
        input = input.Replace("3", "6");
        input = input.Replace("4", "5");
        input = input.Replace("5", "4");
        input = input.Replace("6", "3");
        input = input.Replace("7", "2");
        input = input.Replace("8", "1");
        input = input.Replace("9", "0");


Suggestions and/or examples would be more than welcome!

share|improve this question

closed as off topic by svick, Jesse C. Slicer, Jeff Vanzella, palacsint, dreza Jan 16 '13 at 7:39

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

If someone suggested Code Review for this to you on SO, they were wrong. CR is for reviewing code that works as intended. For more information, read the FAQ. –  svick Jan 15 '13 at 22:38
add comment

1 Answer 1

Simplistic iterative implementation:

        string input = "a29z3";
        StringBuilder output = new StringBuilder();

        foreach (char c in input)
            if ((c >= 'a') && (c <= 'z'))
                output.Append((char)('z' - c + 'a'));
            else if ((c >= '0') && (c <= '9'))
                output.Append((char)('9' - c + '0'));


LINQy implementation:

        string input = "a29z3";

        input = input.Aggregate(string.Empty, (s, c) => s + ((c >= 'a') && (c <= 'z') ? (char)('z' - c + 'a') : (c >= '0') && (c <= '9') ? (char)('9' - c + '0') : c));
share|improve this answer
Dangit, svick is right in his comment that this is not the forum for fixing broke code, SO is. –  Jesse C. Slicer Jan 15 '13 at 22:44
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.