提示。你爲什麼不把字符串拆分成令牌?然後,每次有夫人,先生等詞語倒數倒數。
或用空格替換特殊詞,然後計算沒有問題。
std::string RemoveWords(const std::string& source, const std::string& chars) {
std::string result="";
for (unsigned int i=0; i<source.length(); i++) {
bool foundany=false;
for (unsigned int j=0; j<chars.length() && !foundany; j++) {
foundany=(source[i]==chars[j]);
}
if (!foundany) {
result+=source[i];
}
}
return result;
}
int number_of_sentences = 0;
text = RemoveWords(text);
for(unsigned int i=0; i <= text.length()-1; i++){
if(text[i] == '.' || text[i] == '?' ||text[i] == '!'){
++number_of_sentences;
}
}
return number_of_sentences;
將上述溶液將省略在第二個參數字符串傳遞的每個字符。例如:
std::string result=RemoveWords("Mrs. Rease will play to football. ByeBye", "Mrs.");
爲這些詞添加例外。完成。 – usr2564301 2014-09-06 16:30:18
我需要閱讀的文字很長,這些文字只是一個例子。文中有一些縮寫,我不能在這裏列出。 – 2014-09-06 16:35:11
「請幫忙嗎?」並不是一個簡潔的問題。這不是一對結對的編程服務。 – 2014-09-06 18:07:22