0
0
C++programming~20 mins

String functions overview in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Functions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of string find function
What is the output of this C++ code snippet?
C++
#include <iostream>
#include <string>

int main() {
    std::string s = "hello world";
    std::cout << s.find("world") << std::endl;
    return 0;
}
A11
B-1
C0
D6
Attempts:
2 left
💡 Hint
Remember that string::find returns the index of the first character of the found substring or std::string::npos if not found.
Predict Output
intermediate
2:00remaining
Result of string substr function
What does this C++ code print?
C++
#include <iostream>
#include <string>

int main() {
    std::string s = "programming";
    std::cout << s.substr(3, 4) << std::endl;
    return 0;
}
Agram
Bprog
Cming
Dramm
Attempts:
2 left
💡 Hint
substr(pos, len) returns a substring starting at pos with length len.
Predict Output
advanced
2:00remaining
Output of string compare function
What is the output of this C++ program?
C++
#include <iostream>
#include <string>

int main() {
    std::string a = "apple";
    std::string b = "apricot";
    std::cout << a.compare(b) << std::endl;
    return 0;
}
A-1
Bpositive number
Cnegative number less than -1
D0
Attempts:
2 left
💡 Hint
string::compare returns 0 if equal, positive if first string is lexicographically greater, negative if smaller.
Predict Output
advanced
2:00remaining
Effect of string erase function
What is the output of this C++ code?
C++
#include <iostream>
#include <string>

int main() {
    std::string s = "abcdef";
    s.erase(2, 3);
    std::cout << s << std::endl;
    return 0;
}
Aabdef
Babf
Cabcdef
Dab
Attempts:
2 left
💡 Hint
erase(pos, len) removes len characters starting at pos.
🧠 Conceptual
expert
2:00remaining
Behavior of string::npos in find function
What is the value and meaning of std::string::npos in C++ string functions?
AIt is the index 0, meaning the start of the string.
BIt is a constant representing the maximum possible size_t value, used to indicate 'not found' in string operations.
CIt is a negative integer used to indicate an error in string functions.
DIt is a boolean true value returned when a substring is found.
Attempts:
2 left
💡 Hint
Check the documentation for string::npos and its role in find functions.