Reverse a string

Madhu.b.rokkam
Posted by Madhu.b.rokkam under C# category on | Points: 40 | Views : 1631
We can reverse a string as below

       public string Reverse(string inputValue)
{
char[] charArr = inputValue.ToCharArray();
Array.Reverse(charArr);
return new string(charArr);
}

or


public static string Reverse(string inputValue)
{
StringBuilder sb = new StringBuilder();
for (int i = inputValue.Length-1; i >= 0; i--)
{
sb.Append(inputValue[i]);
}
return sb.ToString();
}


its simple..

Comments or Responses

Posted by: Tripati.patro on: 2/21/2011 Level:Starter | Status: [Member] | Points: 10
Good One.
Posted by: Madhu.b.rokkam on: 2/21/2011 Level:Bronze | Status: [Member] [MVP] | Points: 10
Thanks Tripati
Posted by: T.saravanan on: 2/22/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Madhu,

Nice Try...
Change the loop variable 'i' because in our website 'i' is consider as italic format.So change
for (int i = inputValue.Length-1; i >= 0; i--) to for (int x = inputValue.Length-1; x >= 0; x--)

Try this way to reverse a string...
string s = "ABCDEF";

StringBuilder sb = new StringBuilder();
foreach(char c in s)
{
sb.Insert(0,c);
}
Console.WriteLine(sb);


Cheers :)


Posted by: Madhu.b.rokkam on: 2/22/2011 Level:Bronze | Status: [Member] [MVP] | Points: 10
thanks for ur inputs saravanan

Login to post response