0
0
C++programming~10 mins

Character vs string comparison in C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compare a character variable with a character literal.

C++
char ch = 'a';
if (ch == [1]) {
    std::cout << "Match" << std::endl;
}
Drag options to blanks, or click blank then click option'
A"a"
B'b'
C'a'
D"b"
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes instead of single quotes for characters.
2fill in blank
medium

Complete the code to compare a std::string variable with a string literal.

C++
#include <string>
std::string s = "hello";
if (s == [1]) {
    std::cout << "Equal" << std::endl;
}
Drag options to blanks, or click blank then click option'
A"hello"
B'hello'
C'world'
D"world"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for string literals.
3fill in blank
hard

Fix the error in the code comparing a char and a string literal.

C++
char ch = 'x';
if (ch == [1]) {
    std::cout << "Yes" << std::endl;
}
Drag options to blanks, or click blank then click option'
A'x'
B'y'
C"y"
D"x"
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing char with string literal using double quotes.
4fill in blank
hard

Fill both blanks to create a map with string keys and char values, filtering keys starting with 'a'.

C++
#include <map>
#include <string>
#include <iostream>

std::map<std::string, char> filtered = {
    {"apple", 'a'},
    {"banana", 'b'},
    {"apricot", 'a'}
};

std::map<std::string, char> result = {
    {k, v} for auto& [k, v] : filtered if k[1] [2] 'a'
};
Drag options to blanks, or click blank then click option'
A.at(0)
B[0]
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using .at(0) instead of [0].
Using != instead of == for filtering.
5fill in blank
hard

Fill all three blanks to create a map of uppercase keys with values greater than 10.

C++
#include <map>
#include <string>
#include <cctype>

std::map<std::string, int> data = {
    {"one", 1},
    {"two", 20},
    {"three", 30}
};

std::map<std::string, int> filtered = {
    [1]: v for (const auto& [k, v] : data) if v [2] 10
};

for (const auto& [k, v] : filtered) {
    std::cout << k << ": " << v << std::endl;
}
Drag options to blanks, or click blank then click option'
Ak
B>
Cstd::toupper(k[0]) + k.substr(1)
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of transformed key.
Using < instead of > for filtering.