Question

In: Computer Science

Create a Visual Studio console project using c++ void lowerToUpper(std::string & sentence) that iterates over all...

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)

Solutions

Expert Solution

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!


Related Solutions

Create a C# .NET Core Console project in Visual Studio. (This is the same kind of...
Create a C# .NET Core Console project in Visual Studio. (This is the same kind of project we have been doing all semester.) Do all of the following in the Program class. You do not need to add any other classes to this project. 2. If it exists, remove the Console.WriteLine(“Hello World!”); line that Visual Studio created in the Program class. 3. At the very top of the Program.cs page you should see using System; On the empty line below...
Create a Visual Studio console project (c++) containing a main() program that declares a const int...
Create a Visual Studio console project (c++) containing a main() program that declares a const int NUM_VALUES denoting the array size. Then declare an int array with NUM_VALUES entries. Using a for loop, prompt for the values that are stored in the array as follows: "Enter NUM_VALUES integers separated by blanks:" , where NUM_VALUES is replaced with the array size. Then use another for loop to print the array entries in reverse order separated by blanks on a single line...
Create a new Visual Studio console project named assignment042, and translate the algorithm you developed in...
Create a new Visual Studio console project named assignment042, and translate the algorithm you developed in Assignment 04.1 to C++ code inside the main() function. Your program should prompt for a single 9-digit routing number without spaces between digits as follows: Enter a 9-digit routing number without any spaces: The program should output one of: Routing number is valid Routing number is invalid A C++ loop and integer array could be used to extract the routing number's 9 digits. However...
Write a C program The Visual Studio project itself must make its output to the Console...
Write a C program The Visual Studio project itself must make its output to the Console (i.e. the Command Prompt using printf) and it must exhibit the following features as a minimum: 3%: Looping Menu with 3 main actions: View Cars, Sell Car, View Sales Note: A Car is defined by its price and model 3%: Must contain at least three arrays to record sales figures (maximum of 10 Car models) Two for recording the price and model of one...
Create a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function...
Create a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function 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...
Create a C++ project in visual studio. You can use the C++ project that I uploaded...
Create a C++ project in visual studio. You can use the C++ project that I uploaded to complete this project. 1. Write a function that will accept two integer matrices A and B by reference parameters, and two integers i and j as a value parameter. The function will return an integer m, which is the (i,j)-th coefficient of matrix denoted by A*B (multiplication of A and B). For example, if M = A*B, the function will return m, which...
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3...
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results Create 4 buttons to add, subtract, multiply, and divide Prevent the user from entering text in the number fields Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box Create two additional buttons: - One to store data...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that prompts the user to select a type of coffee she likes and 2) returns the object of what she selected to the console. #include "stdafx.h" #include <iostream> using namespace std; // Product from which the concrete products will inherit from class Coffee { protected:    char _type[15]; public:    Coffee()    {    }    char *getType()    {        return _type;   ...
ceate a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function...
ceate a Visual Studio console project named exercise093. In the exercise093.cpp file first define a function 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 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...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT