Write a program that asks the user to enter a sentence and uses a regular expression to check whether the sentence contains more than one space between words. If so, the program should remove the extra spaces. For example, the string "Hello World" should be "Hello World".
What will be an ideal response?
```
#include
#include
#include
using namespace std;
int main()
{ string sentence;
// get the sentence from the user
cout << "Please enter a sentence: ";
getline( cin, sentence );
// check for extra spaces
if ( !( boost::regex_search( sentence, boost::regex( "\\s{2,}" ) ) ) )
cout << "No extra spaces!" << endl;
else
{
// replace any occurrences of multiple spaces with a single space
sentence =
boost::regex_replace( sentence, boost::regex( "\\s{2,}" ), " " );
// display the corrected sentence
cout << "All extra spaces have been removed.\n" << sentence;
} // end else
cout << endl;
} // end main
```
Please enter a sentence: This sentence has too many spaces
All extra spaces have been removed.
This sentence has too many spaces
You might also like to view...
Use find with the –size (Sobell, page 830) criterion to list the names of files in your home directory that are smaller than 100 bytes.
What will be an ideal response?
In a Word document, if you right-click selected text then the Mini toolbar and a shortcut menu display
Indicate whether the statement is true or false
The optical ________ is the most common pointing device for desktop computers.
A. stylus B. scanner C. mouse D. disc
A(n) copyright is a word, phrase, symbol, or design (or a combination of words, phrases, symbols, or designs) that identifies and distinguishes one product or service from another. _________________________
Answer the following statement true (T) or false (F)