Tutorials  /  Linux Basics

Practical Tips for Converting Strings in C++

Ccentron Redaktion · October 2024 ·3 min read ·Linux Basics, Tutorial

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:

Code
toupper(input_string)
Code
#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.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Converting a Character to Uppercase

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

Code
#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:

Code
tolower(input)
Code
#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:

Code
#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!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren