Loop with letter char as the control variable in for loop in CSharp

Description

The following code shows how to loop with letter char as the control variable in for loop.

Example


using System;/*w  ww.j  a va2s .  c o m*/
using System.Collections.Generic;
using System.Text;

public class MainClass {
    static void Main(string[] args) {
        string greetingText = "www.java2s.com";

        for (int i = (int)'z'; i >= (int)'a'; i--) {
            char old1 = (char)i;
            char new1 = (char)(i + 1);
            greetingText = greetingText.Replace(old1, new1);
        }

        for (int i = (int)'Z'; i >= (int)'A'; i--) {
            char old1 = (char)i;
            char new1 = (char)(i + 1);
            greetingText = greetingText.Replace(old1, new1);
        }

        Console.WriteLine("Encoded:\n" + greetingText);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception