Basic Programming C# Examples

C# Program to Reverse Words of a Sentence

The Program in C# Program to Reverse Words of a Sentence is given below:

using System;

namespace ReverseWords
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a sentence to reverse the words: ");
            string inputString = Console.ReadLine();
            string[] words = inputString.Split(' ');
            Array.Reverse(words);
            string reversedSentence = string.Join(" ", words);

            Console.WriteLine("The reversed sentence is: " + reversedSentence);
        }
    }
}

Output:


Hello Codeauri Family, enter a sentence here to reverse the words:keep programming
Okay, the reversed sentence is: programming keep

Pro-Tips💡

Here are the step by step execution of above program:

  1. The using System; statement is a directive that imports the System namespace, which contains the Console and Array classes used in the code.
  2. The namespace ReverseWords is declared, which is a way of organizing code and preventing naming collisions.
  3. The class Program is declared, which is the main class of the code and contains the entry point of the program, the Main method.
  4. The Main method is defined, which is the starting point of the program and contains the logic for reversing the words in a sentence.
  5. The Console.WriteLine(“Enter a sentence to reverse the words: “) statement outputs a prompt to the console asking the user to enter a sentence.
  6. The string inputString = Console.ReadLine() statement reads the input from the user and stores it in the inputString variable.
  7. The string[] words = inputString.Split(‘ ‘) statement splits the input string into an array of strings, with each string representing a word, using the Split method and the space character as a separator.
  8. The Array.Reverse(words) statement reverses the order of the elements in the words array.
  9. The string reversedSentence = string.Join(” “, words) statement concatenates the reversed words into a string, separated by spaces, using the Join method.
  10. The Console.WriteLine(“The reversed sentence is: ” + reversedSentence) statement outputs the reversed sentence to the console.
  11. The Main method ends and the program terminates.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X