c++ - 单词之间只留一个空格?

标签 c++ string transform erase

我有一个程序,它接受一个字符串并对其进行格式化,去掉它的符号并将大小写设置为小写以便它可以被处理。

例子:

"Man, this is super trippy!" -> "man this is super trippy"
"YOU are entering a NEW dimension!" -> "you are entering a new dimenstion"

不幸的是,处理某些字符串会导致单词之间出现额外的空格。

例子:

"Wait a minute -- this is too groovy!" -> "wait a minute  this is too groovy"
"TONIGHT -- we DINE IN hell!" -> "tonight  we dine in hell"

注意到后两个示例中的空格了吗?出于某种原因,在使用这个处理字符串之后:

line.erase(remove(line.begin(), line.end(), toExclude[i]), line.end());
transform(line.begin(), line.end(), line.begin(), tolower);

在遍历字符串时调用 line.erase 以删除字符的特定实例,而转换将整个内容设置为小写。

由于我的程序一直运行良好,直到我得到单词之间带有连字符的字符串,有没有办法确保单词之间的最大空格保持为 1?

最佳答案

Since my program works fine up until I get to strings with hyphens in between words, is there a way to ensure that the maximum whitespaces between words remains at 1?

是的:考虑使用输入字符串流读取输入字符串,而不是使用删除-删除和转换处理输入字符串(我不会发布任何代码,因为这是您教授的要求:))。

关于c++ - 单词之间只留一个空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232557/

相关文章:

c++ - 简单的 'keypress' 程序

C++ std::map 异构函数指针

c++ - std::transform 如何不返回(而不是抛出),只是跳过?

java - 使用java中的cut()方法

c# - 正则表达式捕获外括号之间的所有内容

javascript - CSS 3D变换以制作给定边长的梯形

python - 在 Python 中,如何像 R 一样进行 group by + mutate + ifelse?

android - 在启动时启动 Qt 应用程序 - Android

c++ - 未解析为候选容器的模板运算符模板

c++ - 读取/写入 outfile 错误(可能是简单的修复)