ameizing ameizing - 1 month ago 13
C++ Question

Leaving exactly one whitespace in between words?

I have a program that takes in a string and formats it, ridding it of symbols and setting the case to lower so that it can be processed.

Example:

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


Unfortunately, processing certain strings causes there to be extra whitespaces between words.

Example:

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


Notice the whitespaces in the latter two examples? For some reason, after processing the strings using this:

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


where line.erase is called when iterating through the string to remove specific instances of a char, and transform sets the entire thing to lowercase.

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?

Answer

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?

Yes: instead of processing the input string using erase-remove and transform, consider reading the input string using an input string stream (I will not post any code as this was requested by your professor :) ).

Comments