Lesson 1
String Manipulation in C#: Reversing Individual Words
Introduction

Hello, and welcome! Are you ready to take your string manipulation skills in C# to the next level? Today, we'll explore a task that not only enhances your understanding of strings but also trains your ability to think creatively. The task at hand involves splitting a string into words, then reversing each word as if reflected in a mirror. Intrigued? Let's dive right in!

Task Statement and Description

Consider a string filled with words. Your task is to write a C# function that accepts such a string. It then takes each of those words, reverses their character order, and finally stitches them all together to form a new string with reversed words.

Here's what you need to keep in mind:

  • The input string will contain between 1 and 100 words.
  • Each word is a sequence of characters separated by white space.
  • A word is composed of characters ranging from a to z, A to Z, 0 to 9, or even an underscore _.
  • The given string will not start or end with a space — double spaces will not appear either.
  • After reversing the words, your program should return a single string with the words preserving their original order.

Example

Suppose that the input string is "Hello neat CSharp_123".

The function will work on this in the following fashion:

  • Hello becomes olleH
  • neat becomes taen
  • CSharp_123 becomes 321_prahSC

The function now combines the obtained strings into one string, resulting in "olleH taen 321_prahSC".

Therefore, if Solution("Hello neat CSharp_123") is called, the returned value should be "olleH taen 321_prahSC".

Let's start breaking this down!

Step-by-Step Solution Building: Step 1

Our very first step requires us to separate the words in the sentence. C# provides us with a built-in Split method, which breaks a given string at a specified separator and outputs an array of words. If no argument is provided to the Split method, it defaults to using space as the separator. Here is a sample code to illustrate this:

C#
1// an initial example string 2string inputStr = "Hello neat CSharp_123"; 3 4// split the string into words 5string[] words = inputStr.Split(); 6 7foreach (string word in words) 8{ 9 Console.WriteLine(word); 10}

You will see the following printed out:

1Hello 2neat 3CSharp_123
Step-by-Step Solution Building: Step 2

We've successfully extracted the words from the sentence, but they're not reversed yet. C# provides the Array.Reverse method, which we can use in conjunction with LINQ's Select and ToArray methods. Let's add these lines to our existing code:

C#
1using System; 2using System.Linq; 3 4public class Program 5{ 6 public static void Main() 7 { 8 // an example string 9 string inputStr = "Hello neat CSharp_123"; 10 11 // split the string into words 12 string[] words = inputStr.Split(); 13 14 // reverse each word 15 string[] reversedWords = words.Select(word => new string(word.Reverse().ToArray())).ToArray(); 16 17 foreach (string reversedWord in reversedWords) 18 { 19 Console.WriteLine(reversedWord); 20 } 21 } 22}

Aha! Now you can see the reversed words:

1olleH 2taen 3321_prahSC
Step-by-Step Solution Building: Step 3

Finally, we need to bring these reversed words back together again into a string format, using space as a separator. We get to use String.Join for this. Here's the code to complete our task:

C#
1using System; 2using System.Linq; 3 4public class Program 5{ 6 public static void Main() 7 { 8 string result = Solution("Hello neat CSharp_123"); 9 Console.WriteLine(result); // this will print: 'olleH taen 321_prahSC' 10 } 11 12 public static string Solution(string inputStr) 13 { 14 // split the string into words 15 string[] words = inputStr.Split(); 16 17 // reverse each word 18 string[] reversedWords = words.Select(word => new string(word.Reverse().ToArray())).ToArray(); 19 20 // join the words back together with space as a separator 21 string result = String.Join(" ", reversedWords); 22 23 return result; 24 } 25}
Lesson Summary

Well done! By completing this lesson, you've gained proficiency in manipulating strings in C#, especially when it comes to reversing the order of characters in a word. I hope you're feeling more confident and enthusiastic about your C# skills. Remember, the key to mastery is regular practice. Therefore, take a moment to explore related problems and practice what you’ve learned. It's all part of the joy of learning!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.