TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

Common Interview Question: Not sure Why

How do you reverse a string? This is not a problem in VB.NET, but in C#...it also isn't a problem. :)

char[] c = ("Hello World!").ToCharArray();
Array.Reverse(c);
string reverse = new string(c);
Console.WriteLine(reverse);

 


Digg!

posted on Saturday, February 07, 2004 12:55 PM

Feedback

# re: Common Interview Question: Not sure Why 1/31/2007 3:34 PM Priyadarshan

If you are asked to solve this without using Array.Reverse() and still use the least looping then the solution would be:
char[] chars = ("Hello World").ToCharArray();
int count = c.Length - 1;

for (int i = 0; i <= count / 2; i++)
{
char tmp = c[i];
c[i] = c[count - i];
c[count - i] = tmp;
}

return new String(chars);