Question

Write a program with three functions: upper , lower , and reverse .

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 .


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 1, 2025

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

KFSys
Site Moderator
Site Moderator badge
March 2, 2025

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;
}

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.