In alternate of This tips-[ http://www.dotnetfunda.com/codes/code1684-reverse-of-a-string-without-using-string-reverse-function.aspx ]
we have various ways to use Reverse a string without using any function.
take a look at given ways
Way 1 :-its better to using LINQ
public static string ReverseString(string str)
{
return new string(Enumerable.Range(1, str.Length).Select(i => str[str.Length - i]).ToArray());
}
Ways 2: - Using bitwise XOR
public static string ReverseXor(string s)
{
char[] charArray = s.ToCharArray();
int len = s.Length - 1;
for (int i = 0; i < len; i++, len--)
{
charArray[i] ^= charArray[len];
charArray[len] ^= charArray[i];
charArray[i] ^= charArray[len];
}
return new string(charArray);
}
Ways 3:- recursive approach
private static string Reverse(string str)
{
if (str.Length == 1)
return str;
else
return str[str.Length - 1] + Reverse(str.Substring(0, str.Length - 1));
}
Way 4 :- flexible way
public static string Reverse(string x)
{
char[] charArray = new char[x.Length];
int len = x.Length - 1;
for (int i = 0; i <= len; i++)
charArray[i] = x[len - i];
return new string(charArray);
}
apart of all these method I will recommend myself to use Array.Reverse() function and LINQ