question: write a programme From a given sequence of words to remove words that contain numbers
#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;
}