i am unsure of how to delete the blank spaces & also read the next line? Write a
ID: 641902 • Letter: I
Question
i am unsure of how to delete the blank spaces & also read the next line?
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 currently is
#include
#include
#include
using namespace std;
void firstletter(char[]);
void erasespaces(char[], int size);
void otherletters(char[]);
int main()
{
int size = 100;
char read[size];
cout << "Please enter your input to be changed:";
cin.getline(read, size);
firstletter(read);
erasespaces(read,size);
otherletters(read);
//cout << read;
return 0;
}
void firstletter(char read[])
{
int i = 0;
if(read[i] != ' ')
{
read[i] = toupper(read[i]);
}
}
void erasespaces(char read[], int size)
{
bool space = false;
for(int j = 0; j < strlen(read); j++)
{
if(read[j] != ' ')
{
cout << read[j];
space = false;
}
else if(read[j] == ' ' && space == false)
{
space = true;
cout << " ";
}
}
}
void otherletters(char read[])
{
for(int k = 1; k < 100; k++)
{
read[k] = tolower(read[k]);
}
}
Explanation / Answer
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
void print(char read[], int size);
int main()
{
int size = 100;
char read[size];
cout << "Please enter your input to be changed:";
cin.getline(read, size);
print(read, size);
//cout << read;
return 0;
}
void print(char read[], int size){
int flag = 0;
int first = 0;
for(int i = 0; i < strlen(read); ++i){
flag = 0;
while(i < strlen(read) && (read[i] == ' ' || read[i] == ' ' || read[i] == ' ')){
if(flag == 0){
cout << " ";
flag = 1;
}
++i;
}
if(first == 0){
cout << (char)toupper(read[i]);
first = 1;
}
else{
cout << (char)tolower(read[i]);
}
}
cout << endl;
}
________________________________________________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.