Convert Lower case string to Upper case string

# Program to Convert Lower case string to Upper case string:

As we know that everything in the computer is in Binary Form, So for characters, we have standardized numeric codes and these codes are called ASCII (American Standard Code for Information Interchange) codes and UniCodes.

The ASCII codes range of Upper case A to Z characters are
65 to 90 and lower case a to z are 97 to 122.

So the difference between each upper case character and lower
case character is 97 - 65 = 32,

So If we have a Lower case character and we want to convert
it to Upper case character, then we have to subtract 32 from
it.

and If we have Upper case character and we have to convert
it to Lower case then we have to add 32 to it.

#include <iostream>

using namespace std;

/*
 * upper case A-Z ASCII value are 65-90 and lower case a-z values are 97-122
 * So the difference between each upper case letter and lower case letter is
 * 97 - 65 = 32, So If we have a Lower case character and we want to convert it
 * to Upper case character, then we have to subtract 32 from it and If we have
 * Upper case character and we have to convert it to Lower case then we have to
 * add 32 to it.
 */
int main()
{
    /*
     * Can not use * here as if we create a pointer in that case the pointer
     * will point to the const string literal which is created in Read Only
     * Memory. So we can not chnage it.
     */
    char str[] = "welcome";

    cout << "The original string: " << str << endl;

    for (int i = 0; str[i] != '\0'; ++i)
    {
        cout << "i: " << i << " " << str[i] << endl;

        str[i] = (str[i] - 32);

        cout << "i: " << i << " " << str[i] << endl;
    }

    cout << "string after change to upper case: " << str << endl;
    return 0;
}

# Output of the program:




Note: Full program can be downloaded from the link below:



Comments

Popular posts from this blog

Insert an element in a sorted array

Finding the length of a string in c and c++

Transposition Linear Search