gpt4 book ai didi

c++ - 我写了一个函数来删除句子中的空格,但它只在空格前取字符而不考虑整个句子

转载 作者:行者123 更新时间:2023-11-28 05:28:14 24 4
gpt4 key购买 nike

我已经编写了一个函数来删除句子中的空格,但它只在空格前取字符而不考虑整个句子。如果我运行这个输出是

Enter the string with space/ tab to trim
IamDoing very fine
Length of the String is : 8
IamDoing

我需要做哪些改变得到“IamDoingveryfine”请大家帮忙,代码如下……”

void removeSpace(const string &str)
{
string tempStr;
string p_str = str;
int l_length = p_str.length();
cout<<"Length of the String is : "<<l_length<<endl;
for (int i=0; i<l_length; i++)
{
if(p_str[i] != ' ' || p_str[i] != '\t')
{
tempStr +=p_str[i]; //to add chars into tempstr
}
}
cout<<tempStr<<'\n';
return ;
}

最佳答案

问题不在于 removespace(),问题在于您进行输入时。

应该是这样的:

std::string line;
std::cout << "Enter the string with space/ tab to trim" << std::endl;
std::cin.getline (line);

关于c++ - 我写了一个函数来删除句子中的空格,但它只在空格前取字符而不考虑整个句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40080813/

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