Implement 2 algorithms and place them in separate methods.
1) Given a sentence, write an algorithm to reverse the order of the words.
Example: “My name is Chris” becomes “Chris name is My”.
2) Given the same sentence as in #1, write an algorithm to reverse each individual word, but not their order.
Example: “My name is Chris” becomes “yM eman si sirhC”.
Winner:
public static string W(string s){
char[] c = s.ToCharArray();
Array.Reverse(c);
return O(new string(c));
}
public static string O(string s){
string[] t = s.Split(' ');
Array.Reverse(t);
return string.Join(" ", t);
}
Another:
static void a(string s)
{
int i = s.LastIndexOf(' ');
c.Write(s.Substring(i + 1) + " ");
if (i < 0)
return;
a(s.Remove(i));
}
static void b(string s)
{
int i = s.LastIndexOf(' ');
if(i > 0)
b(s.Remove(i));
s = s.Substring(i + 1);
i = s.Length;
do
{
c.Write(s[--i]);
} while(i > 0)
c.Write(" ");
}
Another:
static void x(string l)
{
d[] b = l.ToCharArray();
a.Reverse(b);
y(new s(b));
}
static void y(string l)
{
s[] f = l.Split(new d[] { ' ' });
int i = f.Length - 1;
do
{
c.Write(f[i] + " ");
} while (--i > -1);
}