Convert String to lowercase in C++

Convert String to lowercase in C++

In this post, we will see how to convert String to lowercase in C++.

We can assume strings to be an array of characters. Each character has a specific ASCII value, and characters with different cases, for example, a and A have different ASCII values.

Ways to Convert a String to Lowercase in C++

We will discuss different methods on how to convert a string to all lowercase characters in C++.

Using the tolower() and transform() function

The tolower() function in C++ returns a given character in lowercase. This function is defined in the cctype header file. We can apply a given function to all elements of an array using the std::transform() function. The transform() is defined in the algorithm header file.

Using this method, we will apply the tolower() function to every character of a string.

See the following example.

Output:

blog

In the above example, we apply the tolower() function to every character of the string s using the std::transform() function and display it.

We can also eliminate the use of the tolower() function by creating our own function that converts a character to lowercase. This function will use the ASCII value to return the corresponding lowercase of a character.

For example,

Output:

blog

In the above example,

  • The fun() function converts a given character to lowercase.
  • We apply the fun() function to every character using the transform() function.

Using the for loop

We can use the for loop to iterate over the string and convert each character to lowercase. To carry out the conversion, we can either use the tolower() function or the user-defined method used in the previous example.

Both are demonstrated below.

Output:

blog
blog

In the above example,

  • We used the for loop to traverse through the string.
  • Every character was converted to lowercase and displayed.
  • In the first loop, we used the tolower() function. In this case, it will return the ASCII value, so we convert it to a character using the char() function.
  • In the second loop, we used the fun() function used in the earlier example.

Using the boost library

The boost library in C++ provides two methods to convert a given string to lowercase. The to_lower() function from this library modifies the original string and converts it into lowercase. The to_lower_copy() function creates a copy of the string and converts it to lowercase.

We will use both these functions below.

Output:

blog
blog

In the above example,

  • We use the to_lower_copy() function to convert the string to lowercase and store it in a new string.
  • The to_lower() function modifies the original string.

That’s all about how to covnert String to lowercase in C++.

Was this post helpful?

Related Posts

  • 19 May

    Get Type of Object in C++

    Table of ContentsWhy Should We Determine the Type of Object in C++: Dynamic DispatchingGet Type of Object in C++ConclusionWas this post helpful? C++ is an object-oriented language where we often interact with objects of different types. The object interaction becomes more interesting when we add runtime polymorphism to it. The runtime polymorphism, also known as, […]

  • 19 May

    Convert Vector to Array in C++

    Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusionWas this post helpful? This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++ConclusionWas this post helpful? The files are used to store the data permanently on a disk. This mitigates […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusionWas this post helpful? An […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusionWas this post helpful? This article discusses the vector and how to print vector in […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.