Tutorial

C++ String to Uppercase and Lowercase

Published on August 3, 2022
author

Safa Mulani

C++ String to Uppercase and Lowercase

In this article, we will dive into the conversion of the input string to Lowercase and Uppercase in C++. C++ String class provides a huge number of built-in functions to perform operations over the input String.


C++ String to Uppercase

C++ String has got built-in toupper() function to convert the input String to Uppercase.

Syntax:

toupper(input_string)

Example:

#include <iostream>
#include <cstring>


using namespace std;

int main()
{
    char arr[] = "Engineering Discipline.";

    cout << "Original String:\n"<< arr<< endl;
    cout<<"String in UPPERCASE:\n";
    for (int x=0; x<strlen(arr); x++)
        putchar(toupper(arr[x]));
    
    return 0;
}

In the above snippet of code, the cstring package contains the String related functions. Further, strlen() function is used to calculate the length of the input string.

The putchar() method is used to display the data on to the screen/console.

Output:

Original String:
Engineering Discipline.
String in UPPERCASE:
ENGINEERING DISCIPLINE.

Converting an input character to Uppercase

We can even convert the characters/string to Uppercase/Lowercase considering the ASCII values of the input characters.

ASCII values for lower case alphabets (a-z):97 - 122

ASCII values for upper case alphabets (A-Z):65 - 92

Example:

#include <iostream>
using namespace std;

int main()
{
   char X;
   cout<<"Enter a character:"; 
   cin>>X;
   X=X-32;
   cout<<"Converted character to UPPERCASE:";
   cout<<X;
   return 0;
}

As seen above, there happens to be a difference of 32 i.e. 97-65 between the range of ASCII values of lowercase and uppercase alphabets.

So in order to convert the input to uppercase, we need to subtract 32 from the ASCII value of the input character.

Output:

Enter a character:f
Converted character to UPPERCASE:F

C++ String to Lowercase

C++ String has got built-in tolower() function to convert the input string to lowercase.

Syntax:

tolower(input)

Example:

#include <iostream>
#include <cstring>


using namespace std;

int main()
{
    char arr[] = "Engineering Discipline.";

    cout << "Original String:\n"<< arr<< endl;
    cout<<"String in lowercase:\n";
    for (int x=0; x<strlen(arr); x++)
        putchar(tolower(arr[x]));
    
    return 0;
}

Output:

Original String:
Engineering Discipline.
String in lowercase:
engineering discipline.

Converting an input character to Lowercase

Example:

#include <iostream>
using namespace std;

int main()
{
   char X;
   cout<<"Enter a character:"; 
   cin>>X;
   X=X+32;
   cout<<"Converted character to lowercase:";
   cout<<X;
   return 0;
}

We need to add 32 to the ASCII value of the input character to convert it to lowercase.

Output:

Enter a character:R
Converted character to lowercase:r

Conclusion

In this article, we have understood the conversion of character and String input to Lowercase and Uppercase in C++. The important thing to note with the ASCII methods is the fact that they’re simply converting the entered characters to ASCII and then back. If someone enters a number instead of a character, you’ll get a random output.

So you can either handle the inputs and make sure that the entered values are actually characters, or simply use the toupper() and tolower() functions. We hope this tutorial has been useful to you. Comment below if you have any questions.


References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Safa Mulani

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 25, 2021

Your explanation is incorrect. The topupper() function accepts a character as its parameter, not a string. Your examples show this - calling toupper() on chars - but your explanation and initial prototype are incorrect (for example, “toupper(string)” which cannot be done).

- Karim Sultan

    Try DigitalOcean for free

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

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    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.