0
0
C++programming~10 mins

String functions overview 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 get the length of the string.

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

int main() {
    std::string text = "Hello";
    std::cout << text.[1]() << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
AgetSize
Bsize
Ccount
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function that doesn't exist like count() or getSize()
2fill in blank
medium

Complete the code to convert the string to uppercase using a loop.

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

int main() {
    std::string text = "hello";
    for (int i = 0; i < text.length(); ++i) {
        text[i] = [1](text[i]);
    }
    std::cout << text << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Atoupper
BtoUpperCase
Cupper
Dtouppercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent functions like toUpperCase or upper
3fill in blank
hard

Fix the error in the code to find the position of a character in the string.

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

int main() {
    std::string text = "apple";
    size_t pos = text.[1]('p');
    std::cout << pos << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Afind
BindexOf
Csearch
Dposition
Attempts:
3 left
💡 Hint
Common Mistakes
Using JavaScript-like functions such as indexOf
4fill in blank
hard

Fill both blanks to create a substring from index 1 to 3.

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

int main() {
    std::string text = "banana";
    std::string part = text.[1]([2], 3);
    std::cout << part << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Asubstr
Bsubstring
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using substring which is not a C++ string function
Wrong starting index
5fill in blank
hard

Fill all three blanks to replace 'cat' with 'dog' in the string.

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

int main() {
    std::string text = "the cat sat";
    size_t pos = text.[1]("cat");
    if (pos != std::string::npos) {
        text.[2](pos, [3], "dog");
    }
    std::cout << text << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Afind
Breplace
C3
Derase
Attempts:
3 left
💡 Hint
Common Mistakes
Using erase instead of replace
Wrong length for replacement