Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

it must be in C++ Chapter 10, Programming Challenge 11: Case Manipulator Write a

ID: 3816767 • Letter: I

Question

it must be in C++

Chapter 10, Programming Challenge 11: Case Manipulator

Write a program with three functions: upper, lower, and reverse. The upper function should accept a pointer to a C-string as an argument. It should step through each character in the string, converting it to upper. The lower function, too, should accept a pointer to a C-string as an argument. It should step through each character in the string converting it to lowercase. Like upper and lower, reverse should also accept a pointer to a string. As it steps through the string, it should test each character to determine whether it is upper-or lowercase. If a character is uppercase, it should be converted to lowercase. Likewise, if a character is lowercase, it should be converted to uppercase.

Test the functions by asking for a string in function main, then passing it to them in the following order: reverse, lower, and upper.

Sample template:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

// SOLUTION

// Constant for array size
const int SIZE = 80;

// Function prototypes
void upper(char[]);
void lower(char[]);
void reverse(char[]);

int main()
{
// Three character arrays
char str1[SIZE], str2[SIZE], str3[SIZE];

// Get a string
cout << "Enter a string: ";
cin.getline(str1, SIZE);

// Copy the string to str2 and str3.
strcpy_s(str2, str1);
strcpy_s(str3, str1);

// Change str1 to uppercase.
cout << "After a call to Upper: ";
upper(str1);
cout << str1 << endl;

// Change str2 to lowercase.
cout << "After a call to Lower: ";
lower(str2);
cout << str2 << endl;

// Reverse the case in str3.
cout << "After a call to Reverse: ";
reverse(str3);
cout << str3 << endl;
return 0;
}

//***********************************************
// Function upper *
// this function converts each character in the *
// str parameter array to uppercase. *
//***********************************************

void upper(char str[])
{

// TODO


}

//***********************************************
// Function lower *
// this function converts each character in the *
// str parameter array to lowercase. *
//***********************************************

void lower(char str[])
{

// TODO

}

//***********************************************
// Function reverse *
// This function steps through each character *
// in the str parameter array, converting each *
// uppercase character to lowercase, and each *
// lowercase character to uppercase. *
//***********************************************

void reverse(char str[])
{

// TODO

}

Explanation / Answer

#include <iostream>
#include <string>
#include <cctype>
#include <string.h>
using namespace std;
// SOLUTION
// Constant for array size
const int SIZE = 80;
// Function prototypes
void upper(char[]);
void lower(char[]);
void reverse(char[]);
int main()
{
// Three character arrays
char str1[SIZE], str2[SIZE], str3[SIZE];
// Get a string
cout << "Enter a string: ";
cin.getline(str1, SIZE);
// Copy the string to str2 and str3.
strcpy(str2, str1);
strcpy(str3, str1);
// Change str1 to uppercase.
cout << "After a call to Upper: ";
upper(str1);
cout << str1 << endl;
// Change str2 to lowercase.
cout << "After a call to Lower: ";
lower(str2);
cout << str2 << endl;
// Reverse the case in str3.
cout << "After a call to Reverse: ";
reverse(str3);
cout << str3 << endl;
return 0;
}
//***********************************************
// Function upper *
// this function converts each character in the *
// str parameter array to uppercase. *
//***********************************************
void upper(char str[])
{
int i = 0;
while(str[i])
{
str[i] = toupper(str[i]);
i++;
}
}
//***********************************************
// Function lower *
// this function converts each character in the *
// str parameter array to lowercase. *
//***********************************************
void lower(char str[])
{
int i = 0;
while(str[i])
{
str[i] = tolower(str[i]);
i++;
}
}
//***********************************************
// Function reverse *
// This function steps through each character *
// in the str parameter array, converting each *
// uppercase character to lowercase, and each *
// lowercase character to uppercase. *
//***********************************************
void reverse(char str[])
{
int i = 0;
while(str[i])
{
if(islower(str[i]))
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
i++;
}
}

Sample run

Enter a string: abCdE
After a call to Upper: ABCDE
After a call to Lower: abcde
After a call to Reverse: ABcDe