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...
answer the following using C# Design and program a Visual Studio Console project in C# that...
answer the following using C# Design and program a Visual Studio Console project in C# that allows your user to enter a number. The program will examine the number to see if it is prime. If it is prime, it will print the next higher prime and the next lower primes to the console. If the number entered by the user is not prime, display a message to that effect. All code should be written by you. Do not copy/paste...
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...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
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...
Create a Visual Studio console project named exercise101. The main() function should prompt for the name...
Create a Visual Studio console project named exercise101. The main() function should prompt for the name of a text file located in the same directory as exercise101.cpp, and search for a word in the text file as follows: Enter text file name: Enter search word: The program should print the number of occurrences of the word in the file: occurrences of were found in If the file could not be opened then display: File not found Use Stream file I/O...
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...
Using Visual Studio in C#; create a grading application for a class of ten students. The...
Using Visual Studio in C#; create a grading application for a class of ten students. The application should request the names of the students in the class. Students take three exams worth 100 points each in the class. The application should receive the grades for each student and calculate the student’s average exam grade. According to the average, the application should display the student’s name and the letter grade for the class using the grading scheme below. Grading Scheme: •...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT