我有一个程序,它接受一个字符串并对其进行格式化,去掉它的符号并将大小写设置为小写以便它可以被处理。
例子:
"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/