Capitalize First Letter of a String in C++

Neha Imran Sep 11, 2022
Capitalize First Letter of a String in C++

This article will go through various approaches to converting a string’s first alphabet to uppercase.

Capitalize First Letter of a String in C++

We will deal with this problem in three different cases:

  1. The string begins with an alphabet.
  2. The string starts with a special character or a number.
  3. The string contains a multibyte starting character, and we need to capitalize each word.

String Contains Alphabet as a First Character

Solutions to this case are straightforward. We can either use the built-in toupper() library function or can use our user-defined implementation.

We have a method called toupper() in C++. This method converts a lowercase alphabet into uppercase.

The goal can be achieved by simply calling this function on the string’s initial index.

Example Code:

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

int main()
{
    string str = "sudo";
    str[0] = toupper(str[0]);
    cout << str;
}

Output:

Sudo

It is always wise to be familiar with the logic behind built-in methods. Let’s look for a solution without using the built-in one.

Lowercase characters from a to z have an ASCII value between 97 and 122, and uppercase characters from A to Z have an ASCII value between 65 and 92. We deduct 32 from the input char’s ASCII value for conversion from lower to upper case.

It is always good to check whether the first character is already an uppercase letter. Otherwise, we may have to face a strange output.

Example Code:

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

int main()
{
    string ML_Model = "artificial neural networks";
    if(ML_Model[0] >= 97 && ML_Model[0]<= 122){ //if it is a lowercase char
        ML_Model[0] = ML_Model[0] - 32;
    }
    cout << ML_Model;
}

Output:

Artificial neural networks

String’s First Letter is a Special Character

We will now discuss a case where there can be some special characters or numbers at the start of the string. In that case, subtracting 32 from the first letter can generate a strange symbol.

To handle such types of strings, we will first use the isalpha method to determine whether or not it is an alphabet. And after this method, we will use the toupper function on a character.

Example Code:

#include <iostream>
using namespace std;

void upperCaseAlphabet (string &str)
{
    for(int i = 0; i <= str.length(); i++)
    {
        if (isalpha(str[i]))
        {
            str[i] = toupper(str[i]);
            break;
        }
    }
}

int main()
{
    string str = "#1pakistan";
    upperCaseAlphabet(str);
    cout << str;
}

Output:

#1Pakistan

String With Multiple Words Containing Multibyte Starting Characters

In previous examples, we were dealing with strings containing only one word. Let’s now work with longer strings containing more words and special characters and capitalize each word of the string.

We’ll use toupper(), isalpha() and isspace() methods to achieve the goal.

  1. toupper() - converts an alphabet character to uppercase.
  2. isalpha() - checks if the character is an alphabet.
  3. isspace() - checks if the character is a space.

Example Code:

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

void upperCaseAlphabet (string &str)
{
    bool flag = true;
    for(int i = 0; i <= str.length(); i++)
    {
        if (isalpha(str[i]) && flag == true)
        {
            str[i] = toupper(str[i]);
            flag = false;
        }
        else if (isspace(str[i]))
        {
            flag = true;
        }
    }
}

int main()
{
    string str = "i $love #\Traveling";
    upperCaseAlphabet(str);
    cout << str;
}

Output:

I $Love #\Traveling

Related Article - C++ String