Question: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 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 letters are changed to lowercases, that is acceptable. Treat a line break as if it were a blank space, in the sense that a line break and any number of blank spaces are compressed to a single blank space. Assume that the sentence ends with a period and contains no other periods. For example, the input
noW iS thE TiMe fOr aLl gOOD MEN TO ComE TO tHe aId oF
ThE CounTRY. should produce the output:
Now is the time for all good men to come to the aid of the country.
My code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
string space(string &s1);
string indent(string &s2);
string spacetab(string &s3);
string user_sentence;
int main() {
cout<<"Please enter the awful sentence to fix"<<endl;
getline(cin,user_sentence,'.');
while(user_sentence[100]!='.'){
space(user_sentence);
indent(user_sentence);
spacetab(user_sentence);
char periodfinder = user_sentence.length();
if(user_sentence[0] != ' ') {
user_sentence[0] = toupper(user_sentence[0]);
}
for (int i=1;i < periodfinder;i++){
user_sentence[i] = tolower(user_sentence[i]);
}
cout<<user_sentence;
return 0;
}
}
string space(string &s1){
char innerloop = s1.length();
for(int j = 0; j<=innerloop;j++){
for(int i = 0; i<=j;i++){
if(s1[i] == ' ' && s1[i+1] == ' '){
s1.erase(s1.begin()+1);
}
else if (s1[0] == ' ') {
s1.erase(s1.begin());
}
else if (s1[i] == '\0' && s1[i-1] == ' '){
s1.erase(s1.end()-1);
}
}
}
return s1;
}
string indent(string &s2){
for(int i =0; i <= s2.length();i++){
if(s2[i] == '\n') {
s2[i]= ' ';
}
}
return s2;
}
string spacetab(string &s3){
for(int i =0; i <= s3.length();i++){
if(s3[i] == '\t') {
s3[i]= ' ';
}
}
return s3;
}
MY OUTPUT WORKS FINE TO FIX Capital letters but not for extra spaces Output1: Please enter the awful sentence to fix the cat in the hat. The cat in the hat 2nd input test: the cat in the hat. Please enter the awful sentence to fix Input: the cat in the hat. Output: T in the hat
Aucun commentaire:
Enregistrer un commentaire