Hi,
I am quite new to C# and programming in general. I am working on an exercise which asked me to write a program that reverses a string. I could not solve this so I looked at the answer which is shown below:
static void Main(string[] args)
{
Console.WriteLine("Enter a string:");
string myString = Console.ReadLine();
string reversedString = "";
for (int index = myString.Length - 1; index >= 0; index--)
{
reversedString += myString[index];
}
Console.WriteLine("Reversed: {0}", reversedString);
It looks straightforward but I dont understand these lines below:
string reversedString = "";
In this case why is an empty string assigned to reversedString?
for (int index = myString.Length - 1; index >= 0; index--)
I understand this loop apart from the 'mystring.Length - 1' part.
reversedString += myString[index];
This bit of code caused the most confusion.
I would appreciate it if someone would help me towards understang this. Also, what is it that I would more practice on in order to make me understand this and similar problems?
Thanks in advance.