How To Convert Lowercase To Uppercase In C++

In computer programming, lower and upper case characters are two types of characters commonly used to depict alphabets and sometimes other symbols and numbers.

Lowercase characters are letters that are usually written in a smaller size and are used by programmers to represent variables, function names and other identifiers.

Example: ‘a’, ‘b’, etc.

 On the other hand, uppercase characters are letters that are majorly written in larger size and are hence used to represent constants or even to emphasise certain parts of the code.

Example: ‘A’, ‘B’, etc.

In this article, we will explore various approaches and learn how to convert lowercase to uppercase in c++.

Why is there a need to know how to convert lowercase to uppercase in c++

Converting lowercase to uppercase is a very common operation used in programming. There are many reasons for which you may need to convert lowercase to uppercase in c++. These reasons include:

  • User input: One common use of converting lowercase to uppercase is when you are working with user input. Users may type the text in lowercase, but you may want to display it in uppercase. This can improve the readability and consistency of your application.
  • Databases: When working with databases or other data sources, the data may be in lowercase, but you may need to display it in uppercase for consistency with other data in your application. For example: your name needs to start with a capital/uppercase letter or character.
  • Input validation: When accepting user input, it may be necessary to validate the input and ensure that it is in the correct format. Converting input to lowercase or uppercase can help ensure that the input is consistent and matches the expected format. We can then compare the input strings easily.
  • Case-insensitive searches: When searching for text data, it may be necessary to perform case-insensitive searches. Converting all characters to lowercase or uppercase can help simplify the search process and ensure that all matches are found.
  • Standardisation: In some cases, it may be necessary to standardise data in text format to ensure compatibility with other systems or applications.

Approaches on how to change lowercase to uppercase in c++

Numerous approaches are present on how to change lowercase to uppercase in c++. There are some approaches listed and discussed below.

  • Approach-1: Using toupper() function
  • Approach-2: Using transform() function
  • Approach-3: Using ASCII values

Lets dive in details for each approach.

Approach-1: Using toupper() function

The toupper() function is a standard library function in C++ that is used to convert a lowercase character to its uppercase character equivalent. This function can be used to convert an entire string from lowercase to uppercase.

Algorithm:

  1. Takes an ASCII character code as its argument
  2. if the character is a lowercase letter, return the corresponding uppercase letter
  3. Otherwise (else), return the original character.

Code:

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

int main()
{
    char str[100];

    cout << "Enter a string: ";
    cin >> str;    //user input of the lowercase string

    int len = strlen(str);    //taking out the length of the string

    for (int i = 0; i < len; i++) {
        //converting all the lowercase characters to uppercase characters
        str[i] = toupper(str[i]);
    }

    cout << "Uppercase string: " << str << endl;

    return 0;
}

Output:

Enter a string: yes
Uppercase string: YES

Explanation:

  1. Declare a character array str with a maximum size of 100 characters.
  2. Then prompt the user to enter a string using cout and cin functions.
  3. Find the length of the string using the strlen() function
  4. Then we use a for loop to iterate over each character in the string.
  5. Then the toupper() function is used to convert each character to uppercase, and store the result back in the str array.
  6. Finally, we output the uppercase string using the cout function.

Approach-2: Using transform() function

The transform() function is a built-in C++ function that can be used to transform lowercase characters to uppercase characters in a string. This function takes three arguments: the beginning of the input range, the end of the input range, and the beginning of the output range and a binary operation that will be toupper() in this case.

Algorithm:

  1. Iterate over each element in the input range.
  2. Apply the binary operation toupper() to each element in the input range and the corresponding element in the output range.
  3. Write the result of the operation to the corresponding element in the output range.
  4. Repeat steps 2 and 3 until all elements in the input range have been processed.

Code:

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

int main() {
    std::string str = "hello world";
    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
   /* transform function takin the required parameters and the binary operation toupper() to convert lowercase characters to uppercase characters in the string str */
    std::cout << str << std::endl;
    return 0;
}

Output:

HELLO WORLD

Explanation:

  1. We first create a std::string object str that contains the string we want to convert to uppercase from lowercase. The string is initialised to “hello world” containing all characters in lowercase.
  2. Then we call the std::transform algorithm with four arguments as mentioned in the explanation.
  3. After the std::transform call, the str string contains the converted string, with all characters in uppercase.
  4. Finally, we output the contents of str using std::cout. The output will be “HELLO WORLD”.

Approach-3: Using ASCII values

Another approach to convert lowercase characters to uppercase characters is to use the ASCII values of the given characters. In ASCII, lowercase characters have higher values than their corresponding uppercase characters. Hence, to convert a lowercase character to uppercase, we can subtract 32 from its ASCII value.

Algorithm:

  1. Specify a string that contains the input string.
  2. Iterate through the array of characters of the string.
  3. For each character of the array, check if it is a lowercase character by comparing its ASCII value with ASCII value of ‘a’ (97) and ASCII value ‘z’ (122).
  4. If the character is not lowercase, leave it as is.
  5. If the character is a lowercase character, subtract its ASCII value from “a” to get the offset from “a”. This gives us a number between 0 and 25.
  6. Add the ASCII value ‘A’ (65) to the offset to get the corresponding capital letter or the uppercase character. This gives us a number between 65 and 90.
  7. Set the current character of the array to the uppercase character.
  8. Continue iterating until all characters have been checked and converted.
  9. Print or output the modified.

Code:

#include <iostream>

using namespace std;

int main() {
    char str[100];

    cout << "Enter a string: ";
    cin >> str;

    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] >= 'a' && str[i] <= 'z') {  //checking if the letter is in lowercase 
            str[i] = str[i] - 'a' + 'A'; //creating offset and adding A to it
        }
    }

    cout << "Uppercase string: " << str << endl;

    return 0;
}

Output:

Enter a string: hello
Uppercase string: HELLO

Explanation:

  1. In this code, we first declare a character array str to store the input string.
  2. We then read the input string into the array using the cin function.
  3. Next, we iterate through the array of characters in the string.
  4. For each character, we check if it is a lowercase letter by comparing its ASCII value to the ASCII values of ‘a’ and ‘z’.
  5. If the character is a lowercase character, we convert it into an uppercase character by subtracting the ASCII value of ‘a’ and adding the ASCII value of ‘A’.
  6. Finally, we print the modified string using the cout function.

Best Approach-Using transform() function

The std::transform function technique is the finest or the best way to convert lowercase characters to uppercase characters in C++. Here are the reasons why:

  • Portable: The C++ Standard Template Library (STL) includes the standard library function that is the transform() function. It can therefore be used on any platform that supports C++, making it portable.
  • Flexible: As a generic algorithm, the transform() function can be used to transform any sequence of components, not simply strings. As a result, it is a flexible tool that can be applied to a range of tasks in addition to changing strings to uppercase characters.
  • Programmer Convenient: The transform() function is simple and clear to use. A string can be changed from lowercase to uppercase with just one line of code.

Hence, It is effective to use the transform() function.

Sample Problems on how to convert from lowercase to uppercase in c++

Sample Problem-1: Using Approach-1

Problem Definition: Priya wants to write a C++ program that takes a string as input from the user and converts all the lowercase characters in the string to uppercase using the toupper() function. The program should then output the resulting uppercase string. Help Priya to write the program.

Sample input: “hello, world!”

Sample output: “HELLO, WORLD!”

Solution: The code to resolve this issue using the toupper() method is as follows:

Code:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    cout << "Enter a string: ";
    getline(cin, str);   //gets the entire line of strings

    for (int i = 0; i < str.length(); i++) {  //iterating through the line of strings
        str[i] = toupper(str[i]);   //converts each character into uppercase
    }

    cout << "Uppercase string: " << str << endl;
    return 0;
}

Output:

Enter a string: Hello, world!
Uppercase string: HELLO, WORLD!

Code Explanation:

  • Firstly, we declare the user input-storing string variable str.
  • The cout statement is used to ask the user to enter a string, and the getline() function is used to read the entire line as input.
  • Using the length of the string, we iterate through each character in the string using the for loop and then use the toupper() function to change it to uppercase.
  • Finally, we use the cout statement to send the result as an uppercase string.
  • When we run this code with the sample input “Hello, world!”, we get the expected output of “HELLO, WORLD!”.

Sample Problem-2: Using Approach-2

Problem Definition: Rama is given a string containing only lowercase alphabets, therefore, write a C++ program for Rama to convert the first letter of each word in the string to uppercase.

Sample input: “my name is rama”

Sample output: “My Name Is Rama”

Solution: The code to resolve this issue using the transform() method is as follows:

Code:

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

int main() {
    std::string str = "my name is rama";

    // converting the first letter of each word to uppercase
    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
   //converted all to uppercase
    for (int i = 1; i < str.length(); i++) {
        if (str[i] >= 'A' && str[i] <= 'Z' && str[i - 1] != ' ') { //excluding the first letter
            str[i] = str[i] + 32; // converting all others to lowercase
        }
    }

    std::cout << "Original string: " << "my name is rama" << std::endl;
    std::cout << "Modified string: " << str << std::endl;

    return 0;
}

Output:

Original string: my name is rama
Modified string: My Name Is Rama

Code Explanation:

  • First, we define the string str, which only contains lowercase letters.
  • Then, we utilise the transform() function’s capability to switch the whole string into a capitalised string.
  • Next, using a for loop, we then iterate through each string character starting with the second character of the words.
  • Within the loop, we verify that the preceding character does not contain a space character and that the current character is an uppercase letter.
  • We first subtract the ASCII value of “A” from the character’s ASCII value if the preceding condition is true. By doing this, the uppercase alphabet is changed into its lowercase character.
  • Finally, both the original and modified strings are printed.
  • When we run this code with the sample input “my name is rama”, we get the expected output of “My Name Is Rama”.

Sample Problem-3: Using Approach-3

Problem Definition: Harish is applying for the post of a software engineer and he needs to submit his details in uppercase characters which are in lowercase right now. Therefore, help Harish to convert his details like his name to uppercase by writing a program for the same in C++.

Sample input: “harish”

Sample output: “HARISH”

Solution: The code to resolve this issue using the ASCII method is as follows:

Code:

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    char str[100];

    cout << "Enter a string: ";
    cin >> str;

    int len = strlen(str);

    for (int i = 0; i < len; i++) {
        if (str[i] >= 'a' && str[i] <= 'z') { //checking if the letter is in lowercase
            str[i] = str[i] - 'a' + 'A'; //updating the letter into uppercase 
        }
    }

    cout << "Uppercase string: " << str << endl;

    return 0;
}

Output:

Enter a string: harish
Uppercase string: HARISH

Code Explanation:

  • To hold the input string, we declare a character array called str.
  • Using cin, we read the input string into the array.
  • With the strlen method, we can measure the string’s length.
  • Using a for loop, we cycle through the string’s array of characters.
  • By comparing each character’s ASCII value to the ASCII values of “a” and “z,” we may determine whether a given character is a lowercase letter.
  • If the character is a lowercase letter, we change it to an uppercase letter by adding the ASCII value for “A” and removing the value for “a” if it is a lowercase letter.
  • Lastly, we use cout to print the updated string to the console.
  • When we run this code with the sample input “harish”, we get the expected output of “HARISH”.

Conclusion

In C++, there are several approaches to convert lowercase to uppercase letters. Here, the approaches for converting a lowercase character to an uppercase character were presented with an algorithm, C++ program, output and its explanation.

The ASCII value method, the toupper() function, and the convert() function are a few of the methods available in C++ for changing lowercase characters to uppercase characters. However, The best approach is to use the transform() function as it offers a more flexible and convenient way.