Answer to Question #27692 in C++ for patrick asare

Question #27692
question: write a programme From a given sequence of words to remove words that contain numbers
Expert's answer
#include <iostream>
#include <sstream>
#include <string>
#include <cctype>

using namespace std;

/* 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;

string remove_words_with_numbers(const string& str)
istringstream ss(str);
string result, word;

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;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be the first!

Leave a comment

Ask Your question

New on Blog