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.

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!

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.

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.

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal