0

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?

yuritsuki
  • 244
  • 1
  • 4
  • 19
  • Based on the code you've provided, you're `using namespace std;`, which is a [bad habit to get into](http://stackoverflow.com/a/1452738/1863564) – Nic Oct 25 '16 at 06:14
  • @QPaysTaxes I understand that, thank you, unfortunately this is the method the professor has requested we do in order to not receive deductions on assignments. – yuritsuki Oct 25 '16 at 06:15
  • @QPaysTaxes, that's a valid point, but if you're adding little else to the discussion, I'd recommend not stating that alone, as it doesn't contribute to the answer or a discussion that leads to an answer. And to do my part: ameizing, I believe you can keep track of what the last character printed was (a space or no) and avoid outputting spaces in certain cases. Another question, what should happen when `"A B - ! "` is entered? – druckermanly Oct 25 '16 at 06:17
  • @user2899162 The point of comments isn't solely to work towards answers; it's to help the asker in a way that wouldn't (necessarily) be appropriate in an answer, or that's more of a hint than an actual answer. See the placeholder text: "Use comments for more information or **suggest improvements**" – Nic Oct 25 '16 at 06:18
  • Maybe you could use [`std::unique`](http://en.cppreference.com/w/cpp/algorithm/unique) with a custom predicate? – Some programmer dude Oct 25 '16 at 06:18

1 Answers1

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?

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 :) ).

utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • I tried doing that but instead it processes the strings with the symbols and such – yuritsuki Oct 25 '16 at 07:51
  • Please convert this into an actual answer; "here's a hint" is great when it's a homework assignment, but not so much for other people looking at this question later. – Nic Oct 26 '16 at 01:12
  • ... unless the people reading the answer are also doing the same homework assignment :) – utnapistim Oct 26 '16 at 09:21