Lesson 2

Unraveling Strings with Nested Loops in C++

Introduction

Hello and welcome to today's C++ lesson! We are going to unravel a compelling challenge that will refine our skills in string manipulation. This lesson will place particular emphasis on nested loops. Prepare yourself for an intriguing adventure 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. Does that sound exciting? Let's dive in!

Task Statement

The task we'll be demonstrating is as follows: We will work with a string representing a sentence in which words are separated by spaces. Your challenge involves creating a C++ function that identifies the odd-indexed characters of words that have an even number of characters, and then combines these characters into a single string, maintaining the order in which they appeared in the sentence.

Consider this example: "Cplusplus is a high-level programming language." The word 'is' has 2 characters (an even number), and we'll select the odd-indexed character from this word, specifically, 's'. Similarly, we'll select 'i', 'h', 'l', 'v', 'l' from 'high-level'. We'll skip the words 'Cplusplus', 'a', 'programming', and 'language.' because they have odd lengths.

If our function is working correctly, it should return "sihlvl". Isn't it fascinating to see what we can extract from a simple sentence?

Solution Building: Step 1

We will commence our solution building process by splitting the sentence into words. To do this, we need to traverse the string and create a new word every time a space is encountered. After that, we'll have a vector of all the words in the sentence.

C++
1#include <vector> 2#include <string> 3 4std::string solution(std::string sentence) { 5 std::vector<std::string> words; 6 std::string word = ""; 7 for (char c : sentence) { 8 if (c == ' ') { 9 words.push_back(word); 10 word = ""; 11 } else { 12 word += c; 13 } 14 } 15 words.push_back(word); // add the last word to the vector 16 // we will proceed progressively 17}
Solution Building: Step 2

We now introduce nested loops: an outer loop that iterates over every single word, and an inner loop that checks every character within each word. First, we'll use an if condition to verify whether a word has an even length. We can determine this by checking whether the length of the word mod 2 equals zero. If it does, the word has an even length!

C++
1std::string solution(std::string sentence) { 2 std::vector<std::string> words; 3 std::string word = ""; 4 for (char c : sentence) { 5 if (c == ' ') { 6 words.push_back(word); 7 word = ""; 8 } else { 9 word += c; 10 } 11 } 12 words.push_back(word); // add the last word 13 14 for (std::string word : words) { 15 if (word.size() % 2 == 0) { // check if the length of the word is even 16 // we are building up our solution gradually 17 } 18 } 19}
Solution Building: Step 3

With our outer loop set, we're ready to implement our inner loop. We aim to iterate only over the odd-indexed characters in each word of even length. To accomplish this, we'll start from an index of 1 and increment by 2 each time. This method ensures our loop only selects the characters at odd indexes.

We'll then append these characters to our result string, which will be returned as our final output.

C++
1#include <iostream> 2#include <vector> 3 4std::string solution(std::string sentence) { 5 std::vector<std::string> words; 6 std::string word = ""; 7 for (char c : sentence) { 8 if (c == ' ') { 9 words.push_back(word); 10 word = ""; 11 } else { 12 word += c; 13 } 14 } 15 words.push_back(word); // add the last word 16 17 std::string result; 18 for (std::string word : words) { 19 if (word.size() % 2 == 0) { // confirms if the length of the word is even 20 for (int i = 1; i < word.size(); i += 2) { // loop over odd-indexed characters 21 result += word[i]; 22 } 23 } 24 } 25 return result; 26} 27 28int main() { 29 std::cout << solution("Cplusplus is a high-level programming language."); 30 return 0; 31}
Lesson Summary

Well done! You've successfully navigated the realms of nested loops to extract specific characters from words within a sentence. You've learned how to analyze a sentence by breaking it down into its constituent words and studying each word at a deeper level. Now, use this knowledge as a solid foundation in your further exploration of nested loops and string manipulations. The key to mastering this topic is practice; the more you apply what you've learned, the more concrete your understanding becomes. Are you excited to continue exploring nested loops, string manipulations, and the endless possibilities of C++? Let's keep diving deeper!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.