Patrick Gareau

Patrick Gareau

  • NA
  • 14
  • 486

Help me understand some code

Mar 4 2019 4:52 PM
using System;
using System.Text;
namespace forgetCode{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the string to reverse :");
string name = Console.ReadLine();
StringBuilder builder = new StringBuilder();
for (int i = name.Length - 1; i >= 0; i--)
{
builder.Append(name[i]);
}
string newName =builder.ToString();
Console.WriteLine(newName);
}
}}
Can someone please explain how this code reverses a string and why would you use stringbuilder over string?

Answers (3)