g.Spring 2017 2018 Final v1 - Word Table Tools ut References Mailings Review Vie
ID: 3914586 • Letter: G
Question
g.Spring 2017 2018 Final v1 - Word Table Tools ut References Mailings Review View Design Layout Tell me what you want to do Part IlI -(21 Marks) 15 Marks] Words: Write a Ctt program that reads a line of text in a C-String t of the maximum size 200. The program will count the number of words that start with an uppercase letter and count the number of words that end with an uppercase letter. 1. Sample Input/ Output: nter a line: TheY werE SleepinG. ords starting with uppercase: 2 rds ending with uppercase: 3 nter a line: Super Mario. SupeR LuigI. rds starting with uppercase: 4 rds ending with uppercase: 2
Explanation / Answer
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
// taking input
string s;
cout << "Enter a line: ";
getline(cin,s);
// declaring variables
istringstream iss(s);
int startuc = 0, enduc = 0;
do
{
string one;
iss >> one;
if(one.length() > 0)
{
// checking the first chracter in the word
if(one[0] >= 'A' && one[0] <= 'Z')
startuc++;
// if it ends with period, checking the last but one for case
if(one[one.length()-1] == '.')
{
if(one[one.length()-2] >= 'A' && one[one.length()-2] <= 'Z')
enduc++;
}
else
{
// otherwise checking the last character of the word
if(one[one.length()-1] >= 'A' && one[one.length()-1] <= 'Z')
enduc++;
}
}
} while (iss);
// printing output
cout << "Words starting with uppercase: " << startuc << endl;
cout << "Words ending with uppercase: " << enduc << endl;
}
/*SAMPLE OUTPUT
Enter a line: They werE SleepinG.
Words starting with uppercase: 2
Words ending with uppercase: 2
Enter a line: Super Mario. SupeR LuigI.
Words starting with uppercase: 4
Words ending with uppercase: 2
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.