Different ways for reversing a string.

dhirenkaunar-15094
Posted by dhirenkaunar-15094 under C# category on | Points: 40 | Views : 1567
str="dotnetfunda"

# Method 1:
char[] strChar= str.ToCharArray();
Array.Reverse(strChar);
return new string(strChar);

# Method 2:
string revString = string.Empty;
for (int i = str.Length - 1; i >= 0; i--)
{
revString += str[i'];
}


Please remove the single quote from the str[i']

Comments or Responses

Posted by: Man2 on: 5/10/2012 Level:Starter | Status: [Member] | Points: 10
string revString = string.Empty;
for (int i = str.Length - 1; i >= 0; i--)
{
revString += str[i];
}

Index was missing in method 2
Posted by: anwarbesa-15403 on: 5/10/2012 Level:Starter | Status: [Member] | Points: 10
Hi.. thank you so much.. there are good methods for reversing string
Posted by: dhirenkaunar-15094 on: 5/10/2012 Level:Starter | Status: [Member] | Points: 10
Thanks..

Login to post response