Q: Write code to reverse a string manually (loop or recursion) without using the 'reverse' keyword.
==========================================================================
internal class Program
{
static void Main(string[] args)
{
// Q: Write code to reverse a string manually (loop or recursion) without using the 'reverse' keyword.
string str = "Cycle";
var charArray = str.ToArray();
int totalLength = charArray.Length;
int halflength = totalLength / 2;
char temp;
for (int i = 0; i < halflength; i++)
{
temp = charArray[i];
charArray[i] = charArray[totalLength - 1 - i];
charArray[totalLength - 1 - i] = temp;
}
//convert the array of string to String
str = new string(charArray);
Console.WriteLine("The reverse String is - " + str);
}
}
No comments:
Post a Comment