Convert a String to Uppercase or LowerCase in C++

In this article, we will discuss different ways to convert a string to upper case or lower case in C++.

Table Of Contents

  • Using STL
  • using Boost String Algorithm Library

Convert a String to Upper Case using STL

C++ provides a function ::toupper() that converts a character to upper case character i.e.

int toupper ( int c );

To convert a complete string to upper case , just Iterate over all the characters in a string and call ::toupper() function each of them i.e.

std::string data = "This is a sample string.";

// convert string to upper case
std::for_each(
    data.begin(),
    data.end(),
    [](char & c) {
        c = ::toupper(c);
    });

Here we iterated over the string using std::for_each and while iterating we passed each character by reference to the passed callback i.e. a lambda function. This lambda function internally calls the ::toupper() to converts the case of each character.

Advertisements

Convert a String to Lower Case using STL

C++ provides a function ::tolower() that converts a character to lower case character i.e.

int tolower ( int c );

To convert a complete string to lower case , just Iterate over all the characters in a string and call ::tolower() function each of them i.e.

std::string data = "THIS IS A SAMPLE STRING.";

// convert string to back to lower case
std::for_each(
    data.begin(),
    data.end(),
    [](char & c) {
        c = ::tolower(c);
    });

Complete Example is as follows,

#include <iostream>
#include <string>
#include <algorithm>

int main() 
{
	std::string data = "This is a sample string.";

	// convert string to upper case
	std::for_each(
        data.begin(),
        data.end(),
        [](char & c) {
		    c = ::toupper(c);
	    });

	std::cout << "In Upper Case : " << data << std::endl;

	// convert string to back to lower case
	std::for_each(
        data.begin(),
        data.end(),
        [](char & c) {
		    c = ::tolower(c);
	    });

	std::cout << "In Lower Case : " << data << std::endl;

}

Output:

In Upper Case : THIS IS A SAMPLE STRING.
In Lower Case : this is a sample string.

::toupper() & ::tolower() only returns modified character if passed argument is actually an alphabet i.e. a-z or A-Z.

Issue with ::toupper() & ::tolower() is that they converts a single character at a time. To convert the case of a complete string we need to write extra code with these. Whereas, Boost String Algorithm Library provides a direct api that can be used to convert the case of a string i.e.

Convert a String to Upper Case using Boost Library

Boost String Algorithm Library provides a function Boost::to_upper() that can convert a complete string or a sequence to upper case i.e.

std::string data = "boost library is simple.";

// convert string to upper case
boost::to_upper(data);

Convert a String to Lower Case using Boost Library

Boost String Algorithm Library provides a function Boost::to_lower() that can convert a complete string or a sequence to upper case i.e.

std::string data = "boost library is simple.";

// convert string to back to lower case
boost::to_lower(data);

Complete example is as follows,

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

int main() {
    std::string data = "boost library is simple.";

    // convert string to upper case
    boost::to_upper(data);

    std::cout << "In Upper case : " << data << std::endl;

    // convert string to back to lower case
    boost::to_lower(data);

    std::cout << "In Lower case : " << data << std::endl;

}

Output:

In Upper case : BOOST LIBRARY IS SIMPLE.
In Lower case : boost library is simple.

Creating a new string after case conversion

boost:to_upper() & boost::to_lower() converts the case of passed string. Whereas, many times we don’t want to modify the original string, we want to create a new string with converted case. For that boost provides to other function that creates a copy of passed string, then converts the case of new string and returns it. i.e.

boost::to_upper_copy()
boost::to_lower_copy()

Checkout complete example as follows,

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

int main() {
    std::string data = "boost library is simple.";

    // convert a new string in upper case
    std::string newData = boost::to_upper_copy(data);

    std::cout << "Old Data = " << data << std::endl;
    std::cout << "New Data = " << newData << std::endl;
}

Output:

Old Data = boost library is simple.
New Data = BOOST LIBRARY IS SIMPLE.

Summary

We learned how to convert a string to lower or uppercase in C++.

Advertisements

Thanks for reading.

2 thoughts on “Convert a String to Uppercase or LowerCase in C++”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top