Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program that will read in a sentence of up to 100 characters and output

ID: 3547823 • Letter: W

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 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.

Explanation / Answer

#include <iostream>
#include <string>
#include <string.h>


using namespace std;


/*
    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 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.
*/
int main()
{
    int i = 0;              //i for counter
        string str;         //String variable

    getline(cin,str);       //Get string from user
    int L = str.length();   //Find length of string
    cout << str << endl;    //Display original string
   
    //Convert all to lower case first.
    for(i = 0; str[i] != ''; i++){
        if (str[i]=='I'){
            if(str[i+1] ==' '){ //Keep the solitary 'I' capitolized.
            }
            else{
               str[i] = tolower(str[i]);
            }
        }
        else{
            str[i] = tolower(str[i]);
        }
    }
  
    //Check the capitolization after a period and extra spaces
    for(i=0;i<=L;i++){
        if(i==0){
            str[i] = toupper(str[i]);   //First letter to uppercase
        }
        if (i==' '){
            if (i+1 == ' '){
                str.erase(i+1,1);       //Erase extra spaces
            }
        }
        if (str[i] == '.'){
            if(i+1>L){   
            }
            else{
                str[i+2] = toupper(str[i+2]);   //Upper case after a period
            }                                   //Unless end of sentence
        }
    }
   
   //Display new string
   cout<<str<<endl;
   return 0;
}
/*
hOw do I fIx This   sentenCe. weLL   I dont KnoW. do You.
*/