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++
#include <vector>
#include <string>

std::string solution(std::string sentence) {
    std::vector<std::string> words;
    std::string word = "";
    for (char c : sentence) {
        if (c == ' ') {
            words.push_back(word);
            word = "";
        } else {
            word += c;
        }
    }
    words.push_back(word);  // add the last word to the vector
    // we will proceed progressively
}

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++
std::string solution(std::string sentence) {
    std::vector<std::string> words;
    std::string word = "";
    for (char c : sentence) {
        if (c == ' ') {
            words.push_back(word);
            word = "";
        } else {
            word += c;
        }
    }
    words.push_back(word);  // add the last word

    for (std::string word : words) {
        if (word.size() % 2 == 0) {  // check if the length of the word is even
            // we are building up our solution gradually
        }
    }
}
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