Converting a C++ string to uppercase and lowercase.

This article will explore how to convert a string to lowercase and uppercase in C++. The C++ String class offers a wide range of built-in functions for manipulating strings.


Convert a C++ string to uppercase.

The C++ String has an inbuilt toupper() function that can be used to convert the input String into uppercase.

Syntax refers to the set of rules and principles that determine the structure and organization of language.

toupper(input_string)

Can you rephrase this for me?

#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 code snippet above, the cstring package includes functions related to Strings. Additionally, the strlen() function is utilized to determine the length of the input string.

The function putchar() is utilized to present the information on the screen or console.

Result:

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

Changing a lowercase character to uppercase.

Taking into account the ASCII values of the input characters, we have the ability to transform the characters/string to either uppercase or lowercase.

The ASCII range for lowercase alphabets (a-z) is from 97 to 122.

The ASCII codes assigned to capital letters (A-Z) range from 65 to 92.

Paraphrase the following in native English:

“I really appreciate your help with this project.”

#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 observed earlier, there is a 32-gap (97-65) between the ASCII values of lowercase and uppercase letters.

To transform the given input into uppercase, it is necessary to deduct 32 from the ASCII code of the provided character.

Result:

Enter a character:f
Converted character to UPPERCASE:F

Convert a C++ string to lowercase.

C++ String contains a built-in function called tolower() that can be used to convert the input string to lowercase.

Only one option for paraphrasing “Syntax” natively:

Structure:

tolower(input)

Can you provide me with an alternative synonym for the word “beautiful”?

#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;
}

Result:

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

Changing the case of an input character to lowercase.

Can you rephrase the sentence “I am going to the grocery store to buy some milk”?
Possible option: “I’m heading to the supermarket to purchase milk.”

#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;
}

To convert the input character to lowercase, we must increase its ASCII value by 32.

Here is one option for paraphrasing the given sentence:

Result:

Enter a character:R
Converted character to lowercase:r

In summary, to conclude

In this article, we have comprehended the transformation of character and String input into lowercase and uppercase within C++. It is crucial to consider that ASCII methods merely convert the given characters into ASCII and then revert them back. If a number is entered instead of a character, the output obtained will be random.

You have two options: either validate the inputs to ensure they are valid characters, or directly utilize the toupper() and tolower() functions. We trust this tutorial has been beneficial. Please feel free to comment below for any inquiries.


Citations

  • C++ String to Uppercase
  • C++ String to LowerCase

 

More tutorials

2D array for C++ implementation(Opens in a new browser tab)

convert string to character array in Java.(Opens in a new browser tab)

How to Delete Elements from an Array in Java(Opens in a new browser tab)

method X is unclear for the type Y in Java(Opens in a new browser tab)

The Python functions ord() and chr()(Opens in a new browser tab)

How to include items to a list in Python(Opens in a new browser tab)

Leave a Reply 0

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