0
0
C++programming~10 mins

String handling basics 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 declare a string variable named greeting.

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

int main() {
    std::string [1] = "Hello";
    std::cout << greeting << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Ahello
Bstr
Cmessage
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the one printed.
Forgetting to include header.
2fill in blank
medium

Complete the code to get the length of the string greeting.

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

int main() {
    std::string greeting = "Hello";
    std::cout << greeting.[1]() << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Asize
Blength
Ccount
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using len() which is not a C++ string method.
Using count() which does not exist for strings.
3fill in blank
hard

Fix the error in the code to concatenate two strings correctly.

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

int main() {
    std::string first = "Hello, ";
    std::string second = "World!";
    std::string combined = first [1] second;
    std::cout << combined << 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 & which is for addresses or references.
Using * which is for multiplication.
4fill in blank
hard

Fill both blanks to create a substring from greeting starting at index 2 with length 3.

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

int main() {
    std::string greeting = "Hello";
    std::string part = greeting.substr([1], [2]);
    std::cout << part << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A2
B3
C1
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the start index and length.
Using incorrect indices causing wrong substring.
5fill in blank
hard

Fill all three blanks to check if the string greeting contains the character 'e' using find method.

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

int main() {
    std::string greeting = "Hello";
    if (greeting.[1]([2]) [3] std::string::npos) {
        std::cout << "Found 'e'" << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Afind
B'e'
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of != in the condition.
Searching for a string instead of a character.