Reverse string in with and without using reverse function using C#

Reverse string in with and without using reverse function in C#

Hello everyone, I'm sharing the code sample for Reverse strings and numbers with and without using the reverse function

The example in detail is given below.

Example 1, Using for loop

/// REVERSE STRIG WITH USING C# FUNCTIONS
public class ReverseStrigs
    static void Main(string[] a)
    {
    Console.WriteLine("Enter string for Reverse");
        string str = Console.ReadLine();

        Console.WriteLine(Reverse(str));
        Console.ReadLine();
    }

    public static string Reverse(string str)
    {
        return new string(str.Reverse().ToArray());
    }
}

/// REVERSE STRIG WITHOUT USING C# FUNCTIONS.
public class ReverseStrigs
    static void Main(string[] ax)
    {
    Console.WriteLine("Enter string for Reverse");
        string str = Console.ReadLine();

        Console.WriteLine(Reverse(str));
        Console.ReadLine();
    }

    public static string Reverse(string str)
    {
        int strLength = str.Length;
        char[] array = new char[strLength];

        for (int i = 0; i < strLength; i++)
        {
            array[i] = str[strLength - 1 - i];
        }
        return new string(array);
    }
}

The output is


Example 2,

Reverse a string without an Inbuilt function in C# Using While Loop,

namespace DemoExample

{

    class Program: ReverseString

    {

        static void Main(string[] args)

        {

            ReverseString objReverse = new ReverseString(); 

            //reverse a string with Inbuilt function in C#

            // With Inbuilt Method Array.Reverse Method

            string resultReverseAStringWith = objReverse.ReverseAStringWithReverseMethod("ANILKUMARSINGH");//Output -"HGNISRAMUKLINA"

 

            //reverse a string without Inbuilt function in C#

            //Using While Loop

            string resultReverseAStringWithout = objReverse.ReverseAStringWithoutReverseMethod("ANILKUMARSINGH");//Output -"HGNISRAMUKLINA"  

        }

      

    }

 

    class ReverseString

    {

        // Reverse a string with Inbuilt function in C#

        // With Inbuilt Method Array.Reverse Method

        public string ReverseAStringWithReverseMethod(string str)

        {

            char[] chars = str.ToCharArray(); 

            Array.Reverse(chars);

            return new string(chars);           

        }

 

        //Reverse a string without Inbuilt function in C#

        //Using While Loop

        public string ReverseAStringWithoutReverseMethod(string str)

        {

            string reverseStr = string.Empty;

            int length= str.Length - 1; 

            while (length >=0)

            {

                reverseStr = reverseStr + str[length];

                length--;

            }

            return reverseStr;

        }

    }

}



ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Reverse string in with and without using reverse function in C# Reverse string in with and without using reverse function in C# Reviewed by Anil Singh on 6:30 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^