In this article we will discuss Reverse a string in C#.
In this article we will not look at the Reverse method provided by the .NET libraries; we will instead try to implement it by our own logic. However we will be using the ToCharArray method in most of our examples.
Reversing a string is a popular question in most campus hiring interviews. There could be several ways one can think of to do this.
1st Way
Using Two Arrays - One to store the input and another for the output and uses two variables to traverse from the beginning and end and assigns the values from the input array to the output array.
Output
2nd Way
Using Swap Array - Here we will use the swapping for the same also if you look at, we are only traversing half of the array.
3rd Way
Using Swap Array - Here we will use any temp variable, so this is for you, here we are using an in-pace swap.
4th Way
Using Swap Array - Here we will use without copy to char array.
Post a Comment