convert each alternate character of a string to uppercase and lowercase in C sharp

Convert each alternate character of a string to uppercase and lowercase in c#- Learn how to convert alternate letter in a string to upper and lowercase with  example program.

For example input string is  VISWANATH,

After converting the alternate letters to uppercase and lowercase ,

Then output of the  input string is  vIsWaNaTh

Another Example,

Input string

rameshVIJAY

Output string

RaMeShViJaY

 c# program to convert each alternate character of a string to uppercase and lowercase

Code

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the input string");
            string str = Console.ReadLine();

            char[] ch = str.ToCharArray();

            StringBuilder strbld = new StringBuilder();

            for (int i = 0; i < ch.Length; i++)
            {
                //converting even index as uppercase
                if (i % 2 == 0)
                {
                    ch[i] = char.ToUpper(ch[i]);
                    
                }
                else
                { ////converting odd index as lowercase
                    ch[i] = char.ToLower(ch[i]);
                }
                strbld.Append(ch[i].ToString());
                
            }



            Console.WriteLine(strbld.ToString());
        }


    }

Output
Please enter the input string

rameshVIJAY

RaMeShViJaY

Related Posts