public string ReverseWords(string sentence)
{
// Split the sentence into words.
var words = sentence.Split(' ');
// Reverse the order of the words.
var reversedWords = words.Reverse().ToArray();
// Join the words back together into a sentence.
return string.Join(" ", reversedWords);
}
This function first splits the sentence into words using the Split() method. It then reverses the order of the words using the Reverse() method. Finally, it joins the words back together into a sentence using the Join() method.
Here is an example of how to use the ReverseWords() function:
C#
string sentence = "This is a sentence.";
string reversedSentence = ReverseWords(sentence);
Console.WriteLine(reversedSentence);
This code will print the following output:
Code snippet
sentence is a This
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a C# function to reverse the order of words in a given sentence:
C#
This function first splits the sentence into words using the Split() method. It then reverses the order of the words using the Reverse() method. Finally, it joins the words back together into a sentence using the Join() method.
Here is an example of how to use the ReverseWords() function:
C#
This code will print the following output:
Code snippet