Javatpoint Logo
Javatpoint Logo

C# program to reverse words in a given string

Software development frequently involves the need to be able to reverse the order of words within a string. A methodical approach is needed to achieve this functionality in the C# programming language. A clever solution to this issue is demonstrated by the supplied C# program. The program reverses the words in a given string in an efficient manner by utilizing basic string manipulation techniques and C#'s array functionality. Users can learn about the workings of array manipulation, string reassembling, and string splitting into substrings to accomplish desired results through this program. This application demonstrates the usefulness of C# for handling such tasks and provides a realistic example of string manipulation concepts.

Example Code:

Let us take a C# example to illustrate how to reverse words in a given string.

Output:

Enter a string:
Hello everyone
Reversed string:
everyone Hello
Enter a string:
I love programming
Reversed string:
programming love I

Explanation:

  • In this example, we first include the System namespace for input/output operations.
  • The program's entry point is defined by a Main method in a Program class.
  • Next, we use WriteLine() inside the Main method to ask the user to enter a string. Use Console.ReadLine() method to read the input string.
  • After that, the words in the string are reversed by invoking the ReverseWords function and providing the input string as an argument.
  • When a string is supplied, the ReverseWords function reverses the order of the string's words.
  • Split is used inside the ReverseWords method to split the input string into an array of words. We employ a space character as the delimiter to divide the string into words.
  • After that, we reverse the word reverseusing Array.
  • Finally, we use string to rejoin the reversed words into a Join, separating words with a space character.
  • The console is used to show the reversed string to the user when it is returned to the Main write Line.

Example 2:

Let us take another C# example to illustrate how to reverse words in a given string.

Output:

Reversed string: stringsample a is This

Conclusion:

In conclusion, the C #program provides a reliable solution for reversing words within a given string. It uses effective algorithms to iterate through the input string to find word boundaries by detecting spaces. It turns the characters of the word before it upside down when it comes across a space, which turns the string upside down altogether. The application uses built-in techniques like Split and Join in addition to custom functions for character reversal to show off C#'s flexibility in string manipulation. The code is able to be read and understood because of its organized logic and distinct variable names. This program provides a hands-on example of C# string manipulation techniques, and is suitable for a variety of text-processing scenarios.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news