C++常见字符串操作面试题含答案与实现.docxVIP

  • 1
  • 0
  • 约3.28千字
  • 约 8页
  • 2026-09-08 发布于四川
  • 举报

C++常见字符串操作面试题含答案与实现.docx

C++常见字符串操作面试题含答案与实现

1.题目:如何实现一个函数,将一个字符串中的所有空格替换为特定字符,如%20?

答案与实现:

```cpp

includeiostream

includestring

std::stringreplaceSpaces(conststd::stringinput,conststd::stringreplacement){

std::stringoutput;

for(charc:input){

if(c==){

output+=replacement;

}else{

output+=c;

}

}

returnoutput;

}

intmain(){

std::stringinput=HelloWorld!WelcometoC++programming.;

std::stringreplaced=replaceSpaces(input,%20);

std::coutOriginalstrin

文档评论(0)

1亿VIP精品文档

相关文档