Use a regular expression to count the number of digits, characters and whitespace characters in a string.
What will be an ideal response?
```
#include
#include
#include
using namespace std;
int main()
{
int digits = 0, words = 0, whitespace = 0;
string sentence, copy;
boost::smatch match;
// get the sentence from the user
cout << "Please enter a sentence: ";
getline( cin, sentence );
// copy the sentence
copy = sentence;
// count the number of digits
while ( boost::regex_search( copy, match, boost::regex( "\\d" ) ) )
{
++digits;
copy = match.suffix();
} // end while
// copy the sentence
copy = sentence;
// count the number of word characters
while ( boost::regex_search( copy, match, boost::regex( "\\w" ) ) )
{
++words;
copy = match.suffix();
} // end while
// copy the sentence
copy = sentence;
// count the number of whitespace characters
while ( boost::regex_search( copy, match, boost::regex( "\\s" ) ) )
{
++whitespace;
copy = match.suffix();
} // end while
// display the results
cout << "\nThere are " << digits << " digits, " << words
<< " word characters and " << whitespace
<< " whitespace characters." << endl;
} // end main
```
Please enter a sentence: this 77 sentence 6 has 555 words 4444 and numbers
There are 10 digits, 40 word characters and 9 whitespace characters.
You might also like to view...
To calculate the mean in Excel, the AVERAGE function is used
Indicate whether the statement is true or false.
A long description is necessary for ____.
A. charts B. graphs C. data-intensive images D. all of the above