How to reverse string without using Reverse() in C# - Programming for Beginners
Reverse string without using any C# function
Recently in one of the interviews I was asked to reverse the string without using Reverse() in .Net. Below is the simple code to demonstrate how to reverse the string with a simple for each loop.
string strValue =
"Interview question for beginner!";
string
reverseValue = string.Empty;
foreach
(char c in
strValue)
{
reverseValue = c + reverseValue; // concantenate the characters in a reverse mode
}
Response.Write(reverseValue);
Output:
How to reverse string without using Reverse() in C# - Programming for Beginners
Reviewed by Ravi Kumar
on
9:23 PM
Rating:

The result is ok.
ReplyDeleteBut why does it reads strValue variable's value in reverse mode? Does foreach () loop reads a string in reverse direction?