0
0
C++programming~10 mins

String input and output in C++ - Interactive Code Practice

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

Complete the code to read a string from the user using std::cin.

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

int main() {
    std::string name;
    std::cout << "Enter your name: ";
    std::cin [1] name;
    std::cout << "Hello, " << name << "!\n";
    return 0;
}
Drag options to blanks, or click blank then click option'
A>>
B<<
C==
D>>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> with std::cin causes a compilation error.
Using == is a comparison operator, not for input.
2fill in blank
medium

Complete the code to read a full line of text (including spaces) into a string variable.

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

int main() {
    std::string sentence;
    std::cout << "Enter a sentence: ";
    std::getline(std::cin, [1]);
    std::cout << "You wrote: " << sentence << "\n";
    return 0;
}
Drag options to blanks, or click blank then click option'
Ainput
Bsentence
Cline
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name that is not declared causes a compilation error.
Using std::cin >> variable reads only until the first space.
3fill in blank
hard

Fix the error in the code to correctly output the string variable message.

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

int main() {
    std::string message = "Welcome!";
    std::cout [1] message [2] std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A+
B>>
C<<
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using >> instead of << causes compilation errors.
Using + does not work with std::cout for output chaining.
4fill in blank
hard

Fill both blanks to create a dictionary (map) comprehension that stores word lengths for words longer than 3 characters.

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

int main() {
    std::vector<std::string> words = {"apple", "cat", "banana", "dog"};
    std::map<std::string, int> lengths;
    for (const auto& word : words) {
        if (word.[1]() > 3) {
            lengths[word] = word.[2]();
        }
    }
    for (const auto& [w, len] : lengths) {
        std::cout << w << ": " << len << "\n";
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Asize
Blength
Ccount
Dsize_t
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() is incorrect because it does not exist for strings.
Using size_t is a type, not a method.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths for words longer than 4 characters.

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

int main() {
    std::vector<std::string> words = {"apple", "cat", "banana", "dog", "elephant"};
    std::map<std::string, int> result;
    for (auto word : words) {
        if (word.[1]() > 4) {
            std::transform(word.begin(), word.end(), word.begin(), ::[2]);
            result[[3]] = word.size();
        }
    }
    for (const auto& [w, len] : result) {
        std::cout << w << ": " << len << "\n";
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Alength
Btoupper
Cword
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size() instead of length() in the condition is acceptable but here only length() is correct.
Using tolower instead of toupper changes to lowercase.