using System.Diagnostics;
public static void Main()
Console.WriteLine("Hello World");
var timer1 = new Stopwatch();
var timer2 = new Stopwatch();
TimeSpan timeTaken = timer1.Elapsed;
string tt = timeTaken.ToString(@"m\:ss\.fff");
Console.WriteLine($"The Time taken for Method-1 is {tt}");
Console.WriteLine($"The Time taken for Method-1 is {timer1.ElapsedMilliseconds}");
ReverseAString_2("Hello World");
TimeSpan timeTaken2 = timer1.Elapsed;
Console.WriteLine($"The Time taken for Method-2 is {timer2.ElapsedMilliseconds}");
string str3 = "Hello World, This is Rocky";
string str3_new = ReverseWords(str3);
Console.WriteLine($"The Original string is {str3}");
Console.WriteLine($"The Reversed string is {str3_new}");
static void ReverseAString_1(string str)
char[] charArray = str.ToCharArray();
int leftPointer=0, rightPointer=str.Length-1;
while(leftPointer < rightPointer)
tmp = charArray[leftPointer];
charArray[leftPointer] = charArray[rightPointer];
charArray[rightPointer] = tmp;
Console.WriteLine($"The Original string is {str}");
Console.WriteLine($"The Reversed string is {new string(charArray)}");
static void ReverseAString_2(string str)
char[] charArray = str.ToCharArray();
char[] charArray_new = new char[charArray.Length];
int counter=charArray.Length-1;
for(int i=0; i< charArray.Length-1; i++)
charArray_new[i] = charArray[counter];
Console.WriteLine($"The Original string is {str}");
Console.WriteLine($"The Reversed string is {new string(charArray_new)}");
static string ReverseWords(string input)
string[] words = input.Split(' ');
for (int i = 0; i < words.Length; i++)
words[i] = ReverseString(words[i]);
return string.Join(" ", words);
static string ReverseString(string s)
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);