C++

Tolower String C++

C++ has a variety of functions for every specific task. There comes a situation when you need to convert a string to some specific case, i.e., upper-case or lowercase. C++ came up with the tolower() and toupper() functions to do this task. Within this article, we will be discussing the use of the “tolower()” method in C++ to convert a string to lowercase. Let’s get started with creating and opening a new C++ in the Ubuntu 20.04 terminal as below.

$ touch tolower.cc

$ nano tolower.cc

Let’s get started with some examples of using the tolower() function in C++. Within our very first example, we will be converting a character to its lowercase using the color() function of C++ for better understandability. So, we have started our new code with some basic libraries, i.e., iostream and cctype. After this, the namespace for C++ has been added, i.e., “std.” The working has been done within the only main() function. It has been initialized with the character variable “var.” The tolower() function simply converts the character ‘M’ to lowercase and saves the value to the character variable “var.” The cout statement is here to display convert lowercase values in the shell using the “var” variable. The code ends here. So, we have to save it with Ctrl+S and quit the file using the Ctrl+X shortcut.

#include <iostream>

#include <cctype>

using namespace std;

int main() {

  char var = tolower('M');

  cout << var;

  return 0;

}

We have to compile this code using the installed g++ compiler of C++. So we did it and executed it with the “./a.out” instruction. The output result displays the lowercase character “m” converted by the tolower() method in the shell console as below.

$ g++ tolower.cc

$ ./a.out

Example 02:

We cannot convert a whole string to lowercase like a single character. This is because a string is a group of characters. Therefore, we have to convert each character to lowercase separately. So, within this example, we will be looking at the ‘for’ loop to convert the whole string to lowercase utilizing the ‘tolower()’ function. So, we have started the code with some basic library headers, i.o. iostream, cctype, and cstring. The standard “std” namespace is here to help us make our syntax valid. The main() method is started with the character type array “Arr” having a long string as its value. As shown, this character type string has some lowercase characters and some uppercase characters.

The first cout statement is here to display the whole original string without changing it within the shell console. The next cout is here to let us know that the string will be converted into lowercase. So, the “for” loop is initiated with 0 and continues up to the length of a string “Arr” using the strlen() function. On each iteration, each character “Arr[i]” will be converted to lowercase using the “tolower()” function and saved to the new character variable “c.” Each character will be printed out with the cout statement using the “c” variable. After the “for” loop, we have used another cout statement to add the line break. The program is complete now for execution.

#include <iostream>

#include <cctype>

#include <cstring>

using namespace std;

int main() {

  char Arr[] = "I LiKe LiNuX PRoGramMinG";

  cout <<"Original String: " << Arr << endl;

  cout <<"Lowercase String: ";

  for (int i = 0; i < strlen(Arr); i++) {

    char c = tolower(Arr[i]);         cout << c;

  }

  cout<<endl;

  return 0;

}

We have compiled the code first with the “g++” compiler and executed it with the
“./a.out” command as below. The resultant output shows the original string value at the first line. After that, it shows the converted lowercase value of a string on the shell as shown.

$ g++ tolower.cc

$ ./a.out

Example 03:

Let’s take a look at another unique method to convert an uppercase string to a lowercase string in C++. We are required to add only a single header library in this code at the start, i.e., bits/stdc++.h, as shown below. The “std” namespace is here again to use the standard syntax for C++ code. The main() function starts with the initialization of a string variable “s” having an upper-case string value, i.e., LINUX PROGRAM. The first cout statement is used here to show the original string value “s” on the shell, i.e., uppercase value. After this, we utilized the transform() method to convert the string “s” to lowercase string.

Within this function, the begin(), end() and tolower() functions are utilized for this purpose. The first two arguments of the transform() function tell us the location of string “s” to be converted, i.e., start and end. The third argument, “s.begin(),” shows the start point of a new array to be constructed for adding the converted characters into it. The tolower() function is then applied to the main string “s” to convert it to lowercase. The last cout statement has been used to display the whole new lowercase string “s” on the shell.

#include <bits/stdc++.h>

using namespace std;

int main() {

  string s = "LINUX PROGRAM";

  cout<<"Original String: "<< s << endl;

  transform(s.begin(), s.end(), s.begin(), ::tolower);

  cout<<"Lowercase String: "<< s << endl;

  return 0;

}

The compilation and execution have been done. The original upper-case string and converted lowercase string are displayed on the shell below.

$ g++ tolower.cc

$ ./a.out

Conclusion:

This article was all about the C++ tolower string function. We have utilized Ubuntu 20.04 for the implementation of different examples. We have discussed the use of the tolower() method very easily in our examples, taking into consideration the choice of our users. These examples can also be amended according to your work.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.