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 uppercase. 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 .
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there,
If you’re looking for a quick way to generate C++ code for this, you can try DigitalOcean’s GenAI Platform! 🚀
With DigitalOcean GenAI, you can spin up your own AI agent and have it generate, test, and refine your C++ code instantly. Just describe the problem, and the AI will generate the functions for you—no need to wait for a response.
Give it a shot and let me know how it works for you! 😃
- Bobby
Heya,
You can use the DigitalOcean AI product for AI-driven code assistance and automation. Check it out here : DigitalOcean Gen AI.
As for answering your question. Below is the C++ program implementing the upper
, lower
, and reverse
functions:
#include <iostream>
#include <cctype> // For toupper() and tolower()
#include <cstring> // For strlen()
using namespace std;
// Function to convert string to uppercase
void upper(char *str) {
while (*str) {
*str = toupper(*str);
str++;
}
}
// Function to convert string to lowercase
void lower(char *str) {
while (*str) {
*str = tolower(*str);
str++;
}
}
// Function to reverse case of each character in the string
void reverse(char *str) {
while (*str) {
if (islower(*str))
*str = toupper(*str);
else if (isupper(*str))
*str = tolower(*str);
str++;
}
}
int main() {
const int SIZE = 100; // Maximum string size
char input[SIZE];
// Asking the user for a string input
cout << "Enter a string: ";
cin.getline(input, SIZE);
// Apply the functions in the required order
reverse(input);
cout << "Reversed case: " << input << endl;
lower(input);
cout << "Lowercase: " << input << endl;
upper(input);
cout << "Uppercase: " << input << endl;
return 0;
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.