This program must be written in C++: Write a program that reads in a sentence of
ID: 3772329 • Letter: T
Question
This program must be written in C++:
Write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence , all strings of two or mor 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 letters are 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 neds 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.
Explanation / Answer
Program:
Sample output:
Code to copy:
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int main()
{
char sentence[101];
int i=0,j=0,length=0;;
cout<<"Enter your sentence: "<<endl;
//read upto 100 characters
cin.get(sentence,100);
//correct the first character for capitalization
if(islower(sentence[0]))
sentence[0]=toupper(sentence[0]);
//correct the starting characters of each word for capitalization
while(sentence[i]!='')
{
if(sentence[i-1]==' ')
{
if(isupper(sentence[i]))
{
sentence[i]=tolower(sentence[i]);
}
}
i++;
}
//to find the length of the sentence
while(sentence[length]!='')
{
length++;
}
//correct spacing
for(i=0;i<=length-1;i++)
{
if(sentence[i]==' ' &&sentence[i+1]==' ')
{
for(j=i+1;j<length;j++)
{
sentence[j]=sentence[j+1];
}
}
}
//correct period. if there is no period,
//add it at the end of the sentence
if(sentence[length-1]!='.')
{
sentence[length]='.';
sentence[length+1]='';
}
cout<<endl<<"After correcting the sentence: "<<endl<<sentence<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.