Reverse of a string without using the Reverse function in C# and VB

This is a simple code snippet for reversing a string without using the Reverse function. For example, we have a string "He is playing in a ground." without using a function, we can reverse it using the code snippet below. Code is given in both C# and VBalso. This will be helpful to beginners. If you have any queries, please feel free to ask me.
example

C#:
class ReverseString
{
public static void Main(string[] args)
{
string Name = "He is playing in a ground.";
char[] characters = Name.ToCharArray();
StringBuilder sb = new StringBuilder();
for (int i = Name.Length - 1; i >= 0; --i)
{
sb.Append(characters[i]);
}
Console.Write(sb.ToString());
Console.Read();
}
}



VB:
Public Shared Sub Main(args As String())
    Dim Name As String = "He is playing in a ground."
    Dim characters As Char() = Name.ToCharArray()
    Dim sb As New StringBuilder()
   For i As Integer = Name.Length - 1 To 0 Step -1
   sb.Append(characters(i))
   Next
   Console.Write(sb.ToString())
  Console.Read()
  End Sub
End Class



No comments:

Post a Comment