MST

星途 面试题库

面试题:C++ 字符串函数应用

在C++中,已知有一个字符串 `std::string str = "Hello, World!"`,请使用字符串函数实现将其中的 `World` 替换为 `C++`,并输出替换后的字符串。要求详细写出代码实现。
46.3万 热度难度
编程语言C++

知识考点

AI 面试

面试题答案

一键面试
#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    size_t pos = str.find("World");
    if (pos != std::string::npos) {
        str.replace(pos, 5, "C++");
    }
    std::cout << str << std::endl;
    return 0;
}