Lesson 2
Mastering Nested Loops and String Manipulation in C#
Introduction

Hello and welcome to today's C# lesson! We are going to embark on an intriguing challenge that will test our abilities in string manipulation using a concept known as nested loops. Prepare yourself for an interesting journey as we explore how to extract odd-indexed characters from each word in a sentence, but only if the word has an even number of characters. Let's get started!

Task Statement

Here is a detailed look at our task: We will work with a string that represents a sentence with words separated by spaces. Your objective is to create a C# function that identifies the odd-indexed characters of words that have an even number of characters. Then, combine these characters into a single string, maintaining the order in which they appear in the sentence.

Let's consider an example: "CSharp is a high-level programming language." Here, the word CSharp has 6 characters (an even number), so we will select the characters at odd indexes — S, a, p. Similarly, select s from is, and i, h, l, v, and l from high-level. The words a, programming, and language. have odd lengths, so they are skipped.

If our function works correctly, it should return "Sapsihlvl." Observe how much information can be extracted from a simple sentence with this process!

Solution Building: Step 1

We start our solution by splitting the sentence into words. In C#, we use the String.Split method to achieve this. The Split method divides the sentence into words at each space, providing us with an array of words.

C#
1using System; 2 3public class Solution 4{ 5 public static string SolutionMethod(string sentence) 6 { 7 string[] words = sentence.Split(' '); 8 // we will proceed progressively 9 } 10}
Solution Building: Step 2

Next, we delve into nested loops: an outer loop iterating over each word and an inner loop checking each character within those words. Firstly, we'll use an if condition to verify if a word has an even length. We find this by using the modulus operator % with 2. If the result is zero, our word has an even length!

C#
1using System; 2 3public class Solution 4{ 5 public static string SolutionMethod(string sentence) 6 { 7 string[] words = sentence.Split(' '); 8 foreach (string word in words) 9 { 10 if (word.Length % 2 == 0) // confirms whether the length of the word is even 11 { 12 // we are building up our solution progressively 13 } 14 } 15 } 16}
Solution Building: Step 3

With the outer loop set, it's time to complete our inner loop. We intend to iterate over only the odd-indexed characters of each word with an even length. We start with an index of 1 and increment by 2 each time. This strategy ensures our loop selects characters at odd indexes.

We use a StringBuilder for efficient string concatenation, which will be returned as our final output.

C#
1using System; 2using System.Text; 3 4public class Solution 5{ 6 public static string SolutionMethod(string sentence) 7 { 8 string[] words = sentence.Split(' '); 9 StringBuilder result = new StringBuilder(); 10 11 foreach (string word in words) 12 { 13 if (word.Length % 2 == 0) // check if length of word is even 14 { 15 for (int i = 1; i < word.Length; i += 2) // loop over odd characters 16 { 17 result.Append(word[i]); 18 } 19 } 20 } 21 return result.ToString(); 22 } 23}
Solution Building: Step 4

Finally, let's add a Main method so you can execute the code and see the function in action. In the Main method, we will define a sample sentence, call the SolutionMethod, and print the result.

C#
1using System; 2using System.Text; 3 4public class Solution 5{ 6 public static string SolutionMethod(string sentence) 7 { 8 string[] words = sentence.Split(' '); 9 StringBuilder result = new StringBuilder(); 10 11 foreach (string word in words) 12 { 13 if (word.Length % 2 == 0) // check if length of word is even 14 { 15 for (int i = 1; i < word.Length; i += 2) // loop over odd characters 16 { 17 result.Append(word[i]); 18 } 19 } 20 } 21 return result.ToString(); 22 } 23 24 public static void Main(string[] args) 25 { 26 string sentence = "CSharp is a high-level programming language."; 27 string result = SolutionMethod(sentence); 28 Console.WriteLine(result); // Output: "Sapsihlvl" 29 } 30}

Now, you can run this code to see the solution in practice. The Main method serves as the entry point of the program, where we execute our logic and display the output.

Lesson Summary

Congratulations! You've successfully navigated the intricacies of nested loops to extract specific information from words within a sentence using C#. You've learned how to process a sentence by breaking it down into its constituent words and examine each word more deeply.

Use this knowledge as a stepping stone in your exploration of nested loops. Practicing more is crucial — the more you apply what you've learned, the more you will reinforce this knowledge. Are you ready to delve deeper into the world of nested loops and string manipulations in C#? Let's dive right in!

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