Tuesday, 17 May 2022

C# code to reserve a string

Most common or you can say frequenctly asked questions in .Net interviews for Beginner to intermediate.  There are several ways in .Net using C# through which you can reverse a given string.
Lets see one bye one.
Suppose we have input string as : hello then output should be olleh

Method 1: Using inbuild array functions.
So in this method we can split the given string into the character arra using string ToCharArray method and then again we can use Array reverse method to reverse the array and then finally convert it into string. Here is program given which you can execute and see the output.
Method 2: Using Loop.
Using for loop we can add the charatcers of string into another array in reverse order and then we can convert that array to string
Method 3: Using LINQ.
Using System.Linq you can reverse a string in single line. See below code sample

There many more methods through which you can reverse string easily. You can explore more ways yourself and can give you feedback or connect with me as a comment below.


Post a Comment