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.

Computer Science & Information Technology

You might also like to view...

Which of the following is NOT true about Photoshop and Flash?

A. Roundtrip editing from Flash to Photoshop is different than from Flash to Fireworks. B. A Photoshop file imported into Flash opens in a roundtrip-specific editing window in Photoshop. C. You can import an image file that was created in Photoshop into Flash and then edit it in Photoshop. D. A Photoshop file imported into Flash opens in a Photoshop editing window.

Computer Science & Information Technology

If a theory is not confirmed during troubleshooting, what is the technician?s next step?

A. Make changes B. Implement the solution C. Establish a new theory or escalate D. Verify full functionality

Computer Science & Information Technology

To have a form returned to a website or to an email address, you apply the _______________ action to a button.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

Many search engines display page titles and descriptions in their search results.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology