Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a string variable named name.
DSA C++
std::string [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than 'name'.
Forgetting to use std::string type.
✗ Incorrect
The variable name is declared as
name using std::string.2fill in blank
mediumComplete the code to assign the value "hello" to the string variable greeting.
DSA C++
std::string greeting = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes.
Not using any quotes around the string.
✗ Incorrect
String literals in C++ must be enclosed in double quotes, so "hello" is correct.
3fill in blank
hardFix the error in the code to get the length of the string word.
DSA C++
int length = word.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len()' which is not a C++ string method.
Using 'count()' which does not exist for strings.
✗ Incorrect
The correct method to get string length in C++ is
length().4fill in blank
hardFill in the blank to create a substring from text starting at index 2 with length 4.
DSA C++
std::string sub = text.[1](2, 4);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'substring' which is not a C++ string method.
Including parentheses in the method name in the blank.
✗ Incorrect
The method is 'substr' and the starting index is 2.
5fill in blank
hardFill all three blanks to create a map of words to their lengths for words longer than 3 characters.
DSA C++
std::map<std::string, int> lengths; for (const auto& [1] : words) if ([2] > 3) lengths[[3]] = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' as the loop variable instead of 'word'.
Using incorrect expressions for length.
✗ Incorrect
The map uses 'word' as key, 'word.length()' as value, and iterates over 'word' in 'words'.
