In: Computer Science
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question.
1) Count the number of letters in the input
2) Change all lower case letters of the sentence to the corresponding upper case
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
// read a sentence from user
cout << "Enter a sentence: ";
getline(cin, input);
// 1) Count the number of letters in the input
int count = 0;
for (int i = 0; i < input.size(); i++)
{
// if current symbol is a letter
if ((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z'))
count++;
}
cout << "\nTotal letters: " << count << endl;
// 2) Change all lower case letters of the sentence to the corresponding upper case
for (int i = 0; i < input.size(); i++)
{
// if current letter is in lowercase
if (input[i] >= 'a' && input[i] <= 'z')
// convert to uppercase
input[i] -= 'a' - 'A';
}
cout << "\nConverted to uppercase: " << input << endl;
}
.
Output:
.