Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday 18 July 2017

String based program


String program #15 (First reverse the string than make the even position character to upper case)

Enter String: imagination
O/p: -
Reverse String: noitanigami
Reverse String: NoItAnIgAm

The program for the string is written in C# programming language and will accept a string as input. The logic is to make the even position character to upper case.
 
String based program by imagination hunt
String based program

Note: If you are new to C# and Console Application. Try to code First C# Program

Note: Read articles on how to use Loops and Conditions.

Let’s find out a simple and easy way to code the program.

Practical Implementation:

using System;

namespace patternProblem.String
{
    class String15      //First reverse the string than make the even position characters to upper case
    {
        public static void Main()
        {
            Console.Write("Enter String: ");
            string str = Console.ReadLine();

            String15 obj = new String15();

            //this line make the string in reverse direction
            string reverseStr = obj.strReverse(str);
            Console.WriteLine("Reverse String: {0}", reverseStr);

            char[] ch = new char[reverseStr.Length - 1];
            for (int i = 0; i < reverseStr.Length - 1; i++)
            {
                if (i % 2 == 0)
                {
                    //Even position
                    ch[i] = Char.ToUpper(reverseStr[i]);
                }
                else
                {
                    //Odd position
                    ch[i] = reverseStr[i];
                }
            }

            string strUpper = new string(ch);
            Console.WriteLine("Reverse String: {0}", strUpper);
        }

        public string strReverse(string inputStr)
        {
            string str = inputStr;

            char[] ch = str.ToCharArray();

            //In-built menthod to reverse the string
            Array.Reverse(ch);
            return new string(ch);

            //User-defined logic to reverse the string
            //for (int i = str.Length - 1; i >= 0; i--)
            //{
            //    rev += str[i].ToString();
            //}
            //return rev;
        }
    }
}

Output:
Based on the input, program will first reverse the string and then make the even position character to upper case. The output is shown below:

Enter String: imagination
Reverse String: noitanigami
Reverse String: NoItAnIgAm
Press any key to continue . . .


For any query, comment us below.

Skip to Main Table String based program

Previous - String based program #14

Next – String based program #16

Click imagination hunt to read latest blogs.


Keep learning and sharing...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...