Search code examples
c#charincrement

Printing result of increment (x++) returns original value instead of incremented one


        string text = "hello";

        char[] ch = text.ToCharArray();
        
        for (var x=0; x<text.Length; x++)
        {
            Console.Write(ch[x]++); 
        }

This is my current code which produces "hello" as output.

Expecting output to be "ifmmp" as every character is incremented by 1.


Solution

  • You're writing and incrementing at the same time, try first incrementing, then writing

    for (x=0; x<text.Length; x++){
                ch[x]++;
                Console.Write(ch[x]);
            }