Review a solution to the reverse words in a sentence challenge
The following solution provided is one of many possible solutions. The approach taken to solve this challenge was to break down the solution into four steps:
- To create the string array
message, split thepangramstring on the space character. - Create a new
newMessagearray that stores a reversed copy of the "word" string from themessagearray. - Loop through each element in the
messagearray, reverse it, and store this element innewMessagearray. - Join "word" strings from the array
newMessage, using a space again, to create the desired single string to write to the console.
The final result of this example solution.
string pangram = "The quick brown fox jumps over the lazy dog";
// Step 1
string[] message = pangram.Split(' ');
//Step 2
string[] newMessage = new string[message.Length];
// Step 3
for (int i = 0; i < message.Length; i++)
{
char[] letters = message[i].ToCharArray();
Array.Reverse(letters);
newMessage[i] = new string(letters);
}
//Step 4
string result = String.Join(" ", newMessage);
Console.WriteLine(result);
This code is merely "one possible solution" because you can take different approaches to various steps in this process. As long as your output matches the following, you succeeded.
ehT kciuq nworb xof spmuj revo eht yzal god
If you were successful, congratulations!
If you had trouble completing this challenge, maybe you should review the previous units before you continue on.