SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Strings
Exercise:
Programming Projects
Question:1 | ISBN:9780132846813 | Edition: 5

Question

Write a program that will read in a sentence of up to 100 characters and output the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence all strings of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Do not worry about proper names; if their first letter is changed to lowercase, that is acceptable. Treat a line break as if it were a blank in the sense that a line break and any number of blanks are compressed to a single blank. Assume that the sentence ends with a period and contains no other periods. For example, the input

the Answer to life, the Universe, and everything

IS 42.

should produce the following output:

The answer to life, the universe, and everything is 42.



TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>  
#include <string>  
using namespace std; 
 
 
int main() 
 
 
{ 
const int MAX_CHARS = 100; 
char userSentence[MAX_CHARS + 1]; 
string replacedSentence = ""; 
char userChar; 
int spaceCount = 0; 
 
 
 cout << "Enter a Sentence: "; 
cin.getline(userSentence, MAX_CHARS + 1); 
 
 
 int i = 0; 
while (i < MAX_CHARS && userSentence[i] != '\0' 
  && userSentence[i] != '.') 
{ 
  if (isspace(userSentence[i]) || userSentence[i] == ' ') 
 
 
   spaceCount++; 
  else 
   spaceCount = 0; 
  if (spaceCount < 2) 
   replacedSentence += tolower(userSentence[i]); 
  i++; 
} 
 
 
  if (replacedSentence.length() <= 5) 
  { 
   cout << "The sentence does not have 5 or more words!" << endl; 
   system("pause"); 
   return 0; 
  } 
else if (replacedSentence.length() > 0) 
 
 
 { 
  replacedSentence.at(0) = toupper(replacedSentence.at(0)); 
  if (replacedSentence[replacedSentence.length() - 1] == ' ' && replacedSentence.length() == 1) 
   replacedSentence == replacedSentence; 
  else if (replacedSentence[replacedSentence.length() - 1] == ' ') 
   replacedSentence[replacedSentence.length() - 1] = '.'; 
  else if (replacedSentence.length() == MAX_CHARS) 
   replacedSentence[MAX_CHARS - 1] = '.'; 
  else 
   replacedSentence += '.'; 
 
 
 
  cout << "The user sentence: " << endl << userSentence << endl; 
  cout << "The replaced sentence: " << replacedSentence << endl; 
} 
 
 
 system("pause"); 
 
 
 return 0; 
 
 
} 

 

0 0

Discussions

Post the discussion to improve the above solution.