/* Returns true if the given string contains digit(s), false otherwise */ bool contains_number(const string& str) { for (int k = 0; k < str.size(); k++) if (isdigit(str[k])) return true; return false; }
while (getline(ss, word, ' ')) & //read next word if (!contains_number(word)) //check if it contains a number { result.append(word); //add it to the resulting string if not result.append(" "); } return result; }
int main() { string str = "A fat 215 cat sat on 14rats and 8ate8 himself"; cout << "Original string:" << endl; cout << '\t' << str << endl;
cout << "A string with words containing numbers removed:" << endl; cout << '\t' << remove_words_with_numbers(str) << endl; return 0; }
Comments
Leave a comment