The Ultimate Guide to Converting Uppercase and Lowercase Letters in C Using ASCII

The Ultimate Guide to Converting Uppercase and Lowercase Letters in C Using ASCII

A Step-by-Step, Simple and Easy Guide

Introduction

In this guide, you will learn the easiest and most direct way of converting uppercase and lowercase letters in C using ASCII

ASCII is an acronym for American Standard Code for Information Interchange. This ASCII assigns a code called the ASCII code to all the keys on the computer keyboard, with each key having a unique code (number). In this guide our emphasis is just on our alphabet keys, that is our letters, the uppercase A - Z as well as the lowercase a - z.

According to this ASCII code, the number assigned to the uppercase and lowercase letters are;

Uppercase KeysASCII CodeLowercase KeysASCII Code
A65a97
B66b98
C67c99
D68d100
E69e101
F70f102
G71g103
H72h104
I73i105
J74j106
K75k107
L76l108
M77m109
N78n110
O79o111
P80p112
Q81q113
R82r114
S83s115
T84t116
U85u117
V86v118
W87w119
X88x120
Y89y121
Z90z122

The information in the table above is all we need in this guide, I will be explaining how useful this table is for converting letters in uppercase and lowercase.

The Constant Value of Conversion

From the table we have in the introductory part of this guide, you will agree with me that there is something the ASCII Code of the uppercase and lowercase have in common. Let's check something using the letters:

Lowercase Key ASCII Code (LKC)Uppercase Key ASCII Code (UKC)LKC - UKCValue
a (97)A (65)97 - 6532
b (98)B (66)98 - 6632
c (99)C (67)99 - 6732

Now if you observe, from the table above, you will observe a constant number, 32 when we subtract the Lowercase key ASCII Code with its corresponding Uppercase key ASCII Code. So let us check something, let's say we want to move from a lowercase letter to its corresponding uppercase:

Lowercase Key ASCII Code (LKC)Constant Value (32)LKC - 32Value
a (97)3297 - 3265
b (98)3298 - 3266
c (99)3299 - 3267

So for every lowercase letter, when we subtract the number 32 from it, it will give us the ASCII Code for its corresponding uppercase value, as shown in the table below.

Now let us check for when we add the constant 32 to the Uppercase letters ASCII Code and see:

Uppercase Key ASCII Code (UKC)Constant Value (32)UKC + 32Value
A (65)3265 + 3297
B (66)3266 + 3298
C (67)3267 + 3299

From the table, you will see that when we add the constant value 32 to the Uppercase key ASCII Code, we will get a value that corresponds to the Lowercase key ASCII Code of the same letter.

So we can confidently say that to convert from a Lowercase letter to an Uppercase letter, we simply just add 32 to the ASCII Code, and to convert from an Uppercase letter to its corresponding Lowercase letter, we subtract 32 from the ASCII Code.

Converting Lowercase and Uppercase Letters

Now we are going to implement all the theoretical explanations on how to convert a lowercase letter and an uppercase letter. I will be writing the C code using Betty's style of commenting and organizing the code.

Betty is a coding style guide for the C programming language. It is developed by the Holberton School of Engineering and is based on the K&R coding style.

K&R refers to the book "The C Programming Language" by Brian Kernighan and Dennis Ritchie.

Converting from Lowercase to Uppercase

Now let us convert a string of characters in lowercase to uppercase:

1. #include <stdio.h>
2. #include <string.h>
3. /**
4. * main - convert string from lowercase to uppercase
5. * returns: 0
6. **/
7. int main()
8. {
9.    int i;
10.   char str[31] = "we all at alx software engineering";
11.    
12.    for(i = 0; str[i]; i++)
13.    {   
14.        if(str[i] >= 'a' && str[i] <= 'z')
15.        {
16.            str[i] -= 32;
17.        }
18.    }
19.    printf("%s\n", str);
20.    return (0);
21. }

From this code, you will be able to convert lowercase letters to uppercase.

The first and second line is used to declare the header files. Line 3 down to 6 is the comment section, the line 7 is to declare our main int. Line 9 is to initialize our integer and line 10 is to initialize and declare a string array having a size of 31. Line 12 is for the For-loop, initialized it with 0, gave it a condition of str[i], and an act of adding one to i for every time it runs. In line 14, we used the conditional statement to check for every letter that is lowercase and afterward if found, then Line 16 is where we used the constant value conversion to convert such lowercase to their corresponding values to uppercase letters. In line 19 we print out our string in uppercase.

Converting from Uppercase to Lowercase

Using the same code base as before, but in this case, the difference is just in line 16, where instead of subtracting 32, we will be adding 32. And also in line 14, where instead of checking for lowercase letters, we will be checking for uppercase letters.

1. #include <stdio.h>
2. #include <string.h>
3. /**
4. * main - convert string from lowercase to uppercase
5. * returns: 0
6. **/
7. int main()
8. {
9.    int i;
10.   char str[31] = "ARE MEANT TO DO HARD THINGS";
11.    
12.    for(i = 0; str[i]; i++)
13.    {   
14.        if(str[i] >= 'A' && str[i] <= 'Z')
15.        {
16.            str[i] += 32;
17.        }
18.    }
19.    printf("%s\n", str);
20.    return (0);
21. }

So this is a typical example of how to apply the constant value 32 to implement the uppercase-lowercase conversion.

Conclusion

This method is not the only method used in the conversion of uppercase to lowercase letters and vice versa. However, it is one of the most direct and understandable methods out there.

My name is Gideon Bature, a student of ALX Software Engineering as of the time I wrote this article, you can connect with me on LinkedIn and Twitter even as I talk about my Software Engineering Journey.