We can achieve it by splitting the string into words and the reverse those, please review the below sample example 1

using System;
					
public class Program
{
	public static void Main()
	{
		string inputText="How to reverse the words in Sentence?";
		string[] words = inputText.Split(' ');
                Array.Reverse(words);
                inputText = String.Join(" ", words);
		Console.WriteLine(inputText);
	}
}

Input: How to reverse the words in Sentence?

Output: Sentence? in words the reverse to How

Example 2:

using System;
using System.Text;
					
public class Program
{
	public static void Main()
	{
	string inputText = "It is better to know how to learn than to know";
        string[] words = inputText.Split(' ');
        StringBuilder sb = new StringBuilder();
        for (int i = words.Length - 1; i >= 0; i--)
        {
            sb.Append(words[i]);
            sb.Append(" ");
        }
        Console.WriteLine(sb);
	}
}

Output: know to than learn to how know to better is It

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *