Search This Blog

Monday 6 July 2015

Print a reverse string in C# or Print a reverse string using c sharp or Print reverse string without using the reverse function in c# or Print a reverse string in c# using Array. Reverse ()

Step 1:- Method

   public void PrintReverseString1()
        {
            Console.WriteLine("Enter the word you want to display in reverse order");

            string Input = Console.ReadLine();

            char[] strChar = Input.ToCharArray();

            string strReverse = "";

            for (int i = strChar.Length - 1; i >= 0; i--)
            {
                strReverse += strChar[i].ToString();
            }
  
            Console.WriteLine(strReverse);
        }

   public void PrintReverseString2()
        {
            Console.WriteLine("Enter the word you want to display in reverse order");

            string Input = Console.ReadLine();

            String reverse1 = "";
            String reverse2 = "";

            for (int i = 0; i < Input.Length; i++)
            {
                reverse1 = Input[Input.Length - 1 - i].ToString();
                reverse2 = reverse2 + reverse1;
            }

            Console.WriteLine(reverse2);
        }

   public void PrintReverseString3()
        {
            string rev1 = null;
            string rev2 = null;

            Console.WriteLine("Enter the word you want to display in reverse order");

            string Input = Console.ReadLine();

            int inputLength = Input.Length;

            for (int i = 1; inputLength >= i; i++)
            {
                rev1 = Input.Substring(inputLength - i, 1);
                rev2 += rev1;
            }
            Console.WriteLine(rev2);
        }

        public void PrintReverseString4()
        {
            Console.WriteLine("Enter the word you want to display in reverse order");

            string Input = Console.ReadLine();

            char[] strChar = Input.ToCharArray();

            Array.Reverse(strChar);

            Console.WriteLine(strChar);
        }

Step 2:- Complete code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Console_Application_Demo
{
    class Program
    {
        public void PrintReverseString1()
        {
            Console.WriteLine("Enter the word you want to display in reverse order");

            string Input = Console.ReadLine();

            char[] strChar = Input.ToCharArray();

            string strReverse = "";

            for (int i = strChar.Length - 1; i >= 0; i--)
            {
                strReverse += strChar[i].ToString();
            }
  
            Console.WriteLine(strReverse);
        }   

  public void PrintReverseString2()
        {
            Console.WriteLine("Enter the word you want to display in reverse order");

            string Input = Console.ReadLine();

            String reverse1 = "";
            String reverse2 = "";

            for (int i = 0; i < Input.Length; i++)
            {
                reverse1 = Input[Input.Length - 1 - i].ToString();
                reverse2 = reverse2 + reverse1;
            }

            Console.WriteLine(reverse2);
        }

  public void PrintReverseString3()
        {
            string rev1 = null;
            string rev2 = null;

            Console.WriteLine("Enter the word you want to display in reverse order");

            string Input = Console.ReadLine();

            int inputLength = Input.Length;

            for (int i = 1; inputLength >= i; i++)
            {
                rev1 = Input.Substring(inputLength - i, 1);
                rev2 += rev1;
            }
            Console.WriteLine(rev2);
        }

        public void PrintReverseString4()
        {
            Console.WriteLine("Enter the word you want to display in reverse order");

            string Input = Console.ReadLine();

            char[] strChar = Input.ToCharArray();

            Array.Reverse(strChar);

            Console.WriteLine(strChar);
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.PrintReverseString1();  
            p.PrintReverseString2();
            p.PrintReverseString3();
            //Using Array.Reverse() function   
            p.PrintReverseString4();


            Console.ReadLine();

        }
    }
}

Example:-


 

No comments:

Post a Comment