In this article, I am going to discuss How to Reverse a String in C# with and without using built-in Methods. Please read our previous article where we discussed Character Occurrence in a String in C# program with some examples. As part of this article, we are going to use the following three approaches to reverse a string C#.
Here, we will take the input as a string from the user, and then we will convert that string in reverse order as shown in the below image.
Reverse a String in C# without using Built-in method:
In the below program, we are using for loop to reverse a string.
Using the for-each loop to reverse a string in C#:
In the following example, we use for each loop to reverse a string in C#.
In the following example, we use the built-in Reverse method of the Array class to reverse a string.
Summary:
I hope this post will be helpful to understand How to Reverse a String in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
- Using For Loop
- Using For Each Loop
- Using the built-in Reverse method of Array class
Here, we will take the input as a string from the user, and then we will convert that string in reverse order as shown in the below image.
Reverse a String in C# without using Built-in method:
In the below program, we are using for loop to reverse a string.
using System; namespace LogicalPrograms { class Program { static void Main(string[] args) { Console.Write("Enter a String : "); string originalString = Console.ReadLine(); string reverseString = string.Empty; for (int i = originalString.Length - 1; i >= 0; i--) { reverseString += originalString[i]; } Console.Write($"Reverse String is : {reverseString} "); Console.ReadLine(); } } }Output:
Using the for-each loop to reverse a string in C#:
In the following example, we use for each loop to reverse a string in C#.
using System; namespace LogicalPrograms { class Program { static void Main(string[] args) { Console.Write("Enter a String : "); string originalString = Console.ReadLine(); string reverseString = string.Empty; foreach (char c in originalString) { reverseString = c + reverseString; } Console.Write($"Reverse String is : {reverseString} "); Console.ReadLine(); } } }Reverse a String Using in-built Reverse Method in C#:
In the following example, we use the built-in Reverse method of the Array class to reverse a string.
using System; namespace LogicalPrograms { class Program { static void Main(string[] args) { Console.Write("Enter a String : "); string originalString = Console.ReadLine(); char[] stringArray = originalString.ToCharArray(); Array.Reverse(stringArray); string reverseString = new string(stringArray); Console.Write($"Reverse String is : {reverseString} "); Console.ReadLine(); } } }In the next article, I am going to discuss how to reverse each word in a given string in C# using different mechanisms. I hope now you understood how to reverse a string with and without using any built-in method in C#.
Summary:
I hope this post will be helpful to understand How to Reverse a String in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.