This is part 1 of the video series in which we will discuss the commonly asked coding questions for C# and ASP.NET in interviews and will get to know their answers in detail with enough practical examples so that you get a strong understanding of the topics.
If you have any other questions that you would like answers to, please add them to the comment section and I will try to create a video for that topic.
If you like the video please hit the like button and subscribe to the channel if you want to see more free content like this one.
With that said let's start.
In this video, we are going to see how we can reverse a string in C#.
This is a very common coding question that gets asked in interviews.
The concept behind reversing a string is very easy.
Think of a string as 10 people standing in line according to their height.
Now if someone asked you to reverse the order of these people, you would take the last person and the first person, and swap their positions, then you would do the same thing to the second and second last, and so on till the order is reversed.
We will do a similar thing to a string.
We will Look at the string as individual characters, take them one by one, and change their position similar to how we did above,
Let's jump straight to visual studio and see how we can do this.
We will convert this string to a char array so that we have all the characters we want to reverse (including spaces)
Then we run a for loop over the characters.
We create 2 variables, I and J, and we want to run the loop till I is less than J, because if I becomes equal to J, that means there is nothing to swap against.
#csharp #csharptutorial #programming
Then we increase I by 1 and decrease J by 1.
And inside the loop, we swap the places.
Finally when the loop ends, we can write this to the console, and that's it, we have a function that reverses a string.
If this was not an interview, the easiest way to reverse a string would be to use the Reverse() method on IEnumerable.
Convert the string to a char array and use the Reverse method on it.
This would give you the reversed characters as IEnumerable.
Just convert this to an array or a list and display this as a string.
I hope you liked this video, if you did, please hit the like button and subscribe to the channel for more content. I will see you all in the next one.
💖 Subscribe to show support: bit.ly/2FAgXzS
☕ Buy me a coffee: www.buymeacoff...
Негізгі бет C# Interview Questions and Answers - Reverse a String In C#
Пікірлер: 14