Complete the code to declare a string variable named greeting.
#include <iostream> #include <string> int main() { std::string [1] = "Hello"; std::cout << greeting << std::endl; return 0; }
The variable name should be greeting to match the output line.
Complete the code to get the length of the string greeting.
#include <iostream> #include <string> int main() { std::string greeting = "Hello"; std::cout << greeting.[1]() << std::endl; return 0; }
len() which is not a C++ string method.count() which does not exist for strings.The correct method to get string length in C++ is length().
Fix the error in the code to concatenate two strings correctly.
#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; }
& which is for addresses or references.* which is for multiplication.In C++, strings are concatenated using the + operator.
Fill both blanks to create a substring from greeting starting at index 2 with length 3.
#include <iostream> #include <string> int main() { std::string greeting = "Hello"; std::string part = greeting.substr([1], [2]); std::cout << part << std::endl; return 0; }
The substring starts at index 2 and has length 3.
Fill all three blanks to check if the string greeting contains the character 'e' using find method.
#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; }
== instead of != in the condition.The find method returns the position or std::string::npos if not found. We check if it is not equal to npos to confirm presence.
