I'm trying to break down a sentence into its words and then output them back in the wrong order.
I can't get to my current error, because the program stops working halfway through the sentence and returns the rest in normal order.
void ReverseString(string input)
{
string[] subs = input.Split(' ');
string[] subsc = subs;
for (int i = 0; i < subs.Length; i++)
{
Console.Write(subs[i] + " ");
}
for (int i = 0; i < subsc.Length; i++)
{
subs[i] = subsc[subsc.Length - 1 - i];
}
Console.WriteLine("");
for (int i = 0; i < subs.Length; i++)
{
Console.Write(subs[i] + " ");
}
}
Usage:
ReverseString("Write a C# program to display the following pattern using the alphabet.");
Results:
Write a C# program to display the following pattern using the alphabet.
alphabet. the using pattern following the display the following pattern using the alphabet.
string.Join(" ", input.Split(" ").Reverse())maybe?i < subs.Length/2. See: C#: Program to swap two numbers.subsc = subsdoes not create a copy ofsubs. It makes them both the same array. If you change one, you change the other. So your algorithm doesn't work