Practical Tips for Converting Strings in C++

Discover efficient techniques for converting strings to uppercase and lowercase in C++ in our new blog post. From built-in functions to ASCII manipulation – learn how to master this common task effortlessly. Dive in and optimize your C++ development with our practical tips!

Conversion to Uppercase

C++ provides a simple method for converting strings to uppercase using the function toupper(), which is part of the C++ String class. Let’s look at the syntax and an example:


 
#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 this example, we iterate through each character of the input string and apply the toupper() function to convert it to uppercase. The output shows the original string followed by its uppercase version.

Converting a Character to Uppercase

Alternatively, individual characters can be converted to uppercase by manipulating their ASCII values. For example:

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

By subtracting 32 from the ASCII value of the input character, we effectively convert it to uppercase.

Conversion to Lowercase

Similar to the conversion to uppercase, converting to lowercase can be done using the tolower() function. Here’s how:


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

In this code, we iterate through each character of the input string and apply the tolower() function to convert it to lowercase.

Converting a Character to Lowercase

Similarly to converting to uppercase, individual characters can be converted to lowercase by adding 32 to their ASCII values. Here’s how:

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

Conclusion

In summary, we have explored various methods for converting strings and characters to both uppercase and lowercase in C++. While ASCII manipulation provides an alternative, built-in functions like toupper() and tolower() offer a more convenient and reliable approach. It’s important to handle inputs carefully to ensure accurate conversions. We hope this guide was informative and helpful. Please leave a comment below if you have any questions or suggestions. Happy coding!

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: