单词之间只留一个空格?
Leaving exactly one whitespace in between words?
我有一个程序可以接收一个字符串并对其进行格式化,去除其中的符号并将大小写设置为小写以便对其进行处理。
示例:
"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 在遍历字符串以删除 char 的特定实例时被调用,转换将整个内容设置为小写。
由于我的程序运行良好,直到我得到单词之间带有连字符的字符串,有没有办法确保单词之间的最大空格保持为 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?
是:考虑使用输入字符串流读取输入字符串,而不是使用擦除-删除和转换处理输入字符串(我不会 post 任何代码,因为这是您教授要求的:) ).
我有一个程序可以接收一个字符串并对其进行格式化,去除其中的符号并将大小写设置为小写以便对其进行处理。
示例:
"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 在遍历字符串以删除 char 的特定实例时被调用,转换将整个内容设置为小写。
由于我的程序运行良好,直到我得到单词之间带有连字符的字符串,有没有办法确保单词之间的最大空格保持为 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?
是:考虑使用输入字符串流读取输入字符串,而不是使用擦除-删除和转换处理输入字符串(我不会 post 任何代码,因为这是您教授要求的:) ).