I have a function that, given a block of text, should remove all punctuation characters, and make all letters lowercase, and eventually, should shift them according to a mono alphabetic cipher. The code below works:
class Cipher {
public:
string keyword;
string decipheredText;
deque<string> encipheredAlphabet;
static bool is_punctuation (char c) {
return c == '.' || c == ',' || c == '!' || c == '\''|| c == '?' || c
== ' ';
}
string encipher(string text) {
Alphabet a;
encipheredAlphabet = a.cipherLetters(keyword);
text.erase( remove_if(text.begin(), text.end(), is_punctuation),
text.end() );
string::iterator it;
for (it = text.begin(); it != text.end(); it++) {
*it = tolower(*it);
// encipher text according to shift
}
return text;
}
};
The problem is, it currently makes two passes over the string, one to remove the punctuation, and one to do all the other stuff. This seems inefficient, since it seems like all the transformations could be accomplished in one pass through the string somehow. Is there a clean way to incorporate the erase-remove idiom with other loop conditions?