gpt4 book ai didi

c++ - 在 C++ 中用 %20 替换字符串中的空格

转载 作者:太空狗 更新时间:2023-10-29 23:28:47 27 4
gpt4 key购买 nike

#include <iostream>
#include <string>

void removeSpaces(std::string );

int main()
{
std::string inputString;
std::cout<<"Enter the string:"<<std::endl;
std::cin>>inputString;

removeSpaces(inputString);

return 0;
}



void removeSpaces(std::string str)
{
size_t position = 0;
for ( position = str.find(" "); position != std::string::npos; position = str.find(" ",position) )
{
str.replace(position ,1, "%20");
}

std::cout<<str<<std::endl;
}

我看不到任何输出。例如

Enter Input String: a b c
Output = a

怎么了?

最佳答案

std::cin>>inputString;

停在第一个空格处。使用:

std::getline(std::cin, inputString);

相反。

关于c++ - 在 C++ 中用 %20 替换字符串中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7391178/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com