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 ,julia Lobur
Chapter:
Strings And Vectors
Exercise:
Programming Projects
Question:1 | ISBN:9780321531346 | Edition: 7

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

using namespace std;
int main()
{ char arr[100];

int len=0, j=0;
string str;

cout<<"Enter string:"<<endl;

getline(cin, str);
len = str.length();

char res[100];

strcpy(arr, str.c_str());

for(int i = 0; i<len; i++){

if(arr[i]==' ' && arr[i+1]==' ')
continue;

if(arr[i]>=65 && arr[i]<=90){
res[j++]=arr[i]+32;

}
else{
res[j++]=arr[i];
}

}
res[0]=res[0]-32;

cout<<"Output string;"<<endl;
cout<<endl;
for(int i=0;i<j;i++)
cout<<res[i];


return 0;
}

1 0

Discussions

Post the discussion to improve the above solution.