In: Computer Science
Create a Visual Studio console project using c++
void lowerToUpper(std::string & sentence)
that iterates over all characters in the sentence argument. Any lowercase letter should be converted to uppercase. This can be done by including <cctype> and testing each character in sentence with the islower() function. If islower(sentence[i]) returns true then sentence[i] should be replaced with toupper(sentence[i]). The main() function should assign "Hello how are you doing?" to sentence, call lowerToUpper(sentence), and use an if statement to check the new value of sentence. If the result is correct then main() should return 0 as usual. If the result is not correct then main() should display a descriptive error message (indicating the function called, argument and the incorrect result) on the console, and main() should return -1. This provides a unit test for the lowerToUpper() function.
Finally, see what happens when you rerun the program after changing the function interface to
void lowerToUpper(std::string sentence)
Answer:-
Hello, I have written the required CPP Program. Please find it below.
CPP CODE:-
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
// LowerToUpper function starts here
void lowerToUpper(std::string & sentence)
{
// Loop for iterating over each character of String
sentence
for(int i = 0; i<sentence.length(); i++) {
// Checking if the character at index i is in lower case or
not
if(islower(sentence[i]))
{
// Converting the character at index i into Upper case
sentence[i]= toupper(sentence.at(i));
}
}
}
//main function starts here
int main()
{
// Declaring String sentence
string sentence = "Hello how are you doing?";
string result = "HELLO HOW ARE YOU DOING?";
// Printing Original String before calling to LowerToUpper
function
cout<<"Original String is :
"<<sentence<<endl;
// Call to LowerToUpper function by passing sentence
string
lowerToUpper(sentence);
// Checking if the sentence is equal to result
String
if(sentence==result)
{
// Printing the sentence String after calling to LowerToUpper
function (in Uppercase)
cout<<"String in Uppercase is
: "<<sentence<<endl;
return 0;
}
else
{
// Printing error message, if the String could not be converted
into Uppercase Successfully
cout<<"Error: - The string
sentence which is passed to LowerToUpper function could not be
converted into uppercase.";
return -1;
}
}
Screenshot of Code:-
Please refer to the screenshots of the code for properly understanding the indentation of the code.
Screenshot 1:-
Screenshot 2:-
OUTPUT:-
Changing the interface of function:-
Now, if we change the function interface to
void lowerToUpper(std::string sentence)
So, here this function would be called by Values (not by reference). Therefore the changes made in sentence String in LowerToUpper function would not affect the sentence String in the main function. So, there would not be any changes in sentence String in the main function.
Since there would not be any change in sentence String in the main function, so it will not be converted into uppercase, therefore, our program would print the error message.
I have included the code below.
CPP CODE:-
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
// LowerToUpper function starts here
void lowerToUpper(std::string sentence)
{
// Loop for iterating over each character of String
sentence
for(int i = 0; i<sentence.length(); i++) {
// Checking if the character at index i is in lower case or
not
if(islower(sentence[i]))
{
// Converting the character at index i into Upper case
sentence[i]= toupper(sentence.at(i));
}
}
}
//main function starts here
int main()
{
// Declaring String sentence
string sentence = "Hello how are you doing?";
string result = "HELLO HOW ARE YOU DOING?";
// Printing Original String before calling to LowerToUpper
function
cout<<"Original String is :
"<<sentence<<endl;
// Call to LowerToUpper function by passing sentence
string
lowerToUpper(sentence);
// Checking if the sentence is equal to result
String
if(sentence==result)
{
// Printing the sentence String after calling to LowerToUpper
function (in Uppercase)
cout<<"String in Uppercase is
: "<<sentence<<endl;
return 0;
}
else
{
// Printing error message, if the String could not be converted
into Uppercase Successfully
cout<<"Error: - The string
sentence which is passed to LowerToUpper function could not be
converted into uppercase.";
return -1;
}
}
Screenshot of Code:-
OUTPUT:-
I hope it would help.
Thanks!