find and solve || findandsolve.com
Please wait.....

How to Reverse a String in C# by User Input

Built-in function to reverse a string in C#. First, the string is converted to a character array by using ToCharArray() then by using the Reverse() the character array will be reversed, But in this article, we will understand the Best way to reverse a string or Reverse a String without using Reverse().


Best way to reverse a string

In this blog, I am going to focus on  Reverse function in C# with and also without using built-in Methods. As part of this blog, I am going to use the following three approaches to reverse a string C#.

  • Using For Loop
  • Using For Each Loop
  • Using the built-in Reverse method of Array class


Reverse a String in C# without using the Built-in method:

using System;
namespace FindAndSolve
{
   public class Program
    {
       public static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string str = Console.ReadLine();
            string revStr = string.Empty;
            for (int i = str.Length - 1; i >= 0; i--)
            {
                revStr += str[i];
            }
            Console.Write($"Reverse String is : {revStr} ");
            Console.ReadLine();
        }
    }
}

Output



Using the for-each loop to reverse a string in C#:

using System;
namespace FindAndSolve
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string str = Console.ReadLine();
            string revStr = string.Empty;
            foreach (char c in str)
            {
                revStr = c + str;
            }
            Console.Write($"Reverse String is : {revStr} ");
            Console.ReadLine();
        }
    }
}

Output



Reverse a String Using in-built Reverse Method in C#:

using System;
namespace LogicalPrograms
{
    public class Program
    {
       public static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string str = Console.ReadLine();
            char[] strArray = str.ToCharArray();
            Array.Reverse(strArray);
            string reverseString = new string(strArray);
            Console.Write($"Reverse String is : {reverseString} ");
            Console.ReadLine();
        }
    }
}

Output



ANOTHER SOLUTION



Solution 1.

public static string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}


Solution 2.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
public static class Test
{
    private static IEnumerable<string> GraphemeClusters(this string str) {
        var enumerator = StringInfo.GetTextElementEnumerator(str);
        while(enumerator.MoveNext()) {
            yield return (string)enumerator.Current;
        }
    }
    private static string ReverseGraphemeClusters(this string str) {
        return string.Join("", str.GraphemeClusters().Reverse().ToArray());
    }
    public static void Main()
    {
        var objVar = "Les Mise\u0301rables";
        var reverse= objVar.ReverseGraphemeClusters();
        Console.WriteLine(reverse);
    }
}


Solution 3.

using Array.Reverse for most cases as it is coded natively and it is very simple to maintain and understand.

public string Reverse(string text)
{
   if (text == null) return null;
   // this was posted by petebob as well
   char[] array = text.ToCharArray();
   Array.Reverse(array);
   return new String(array);
}

There is a second approach that can be faster for certain string lengths which uses Xor.

    public static string ReverseXor(string s)
    {
        if (s == null) return null;
        char[] charArray = s.ToCharArray();
        int len = s.Length - 1;
        for (int i = 0; i < len; i++, len--)
        {
            charArray[i] ^= charArray[len];
            charArray[len] ^= charArray[i];
            charArray[i] ^= charArray[len];
        }
        return new string(charArray);
    }

Solution 4.

Use LINQ (.NET Framework 3.5+) than following a one-liner will give you a shortcode. Don't forget to add using System.Linq; to have access to Enumerable.Reverse:

public string ReverseString(string srtVarable)
{
    return new string(srtVarable.Reverse().ToArray());
}

Or

public string Reverse(string text)
{
   return Microsoft.VisualBasic.Strings.StrReverse(text);
}