C Program To Convert Lowercase To Uppercase

Problem Analysis

Lower case letters are those that do not form the beginning of the sentence. Lower case letters do not form the proper noun. Following are the lower case letters:

 

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

    

Lower case letters are used in following circumstances:

  • To represent common nouns.
  • To form sentences, accept the beginning of the sentences.

   

Upper case letters are those that form the beginning of the sentences. They also form the proper noun. Upper case letters draw attention of the reader to a particular word or sentence. Following are the upper case letters:

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

    

According to the grammar of American/British english every first letter of every sentence is capitalized, thus it forms the beginning of the sentence. Upper case are used in following circumstances:

  • Proper Noun
  • Acronyms
  • Headings/Titles

Problem Description

The problem is to develop a C Program to Convert Lowercase to Uppercase.

American Standard Code for Information Interchange (ASCII) is used to communicate with computers. Using ASCII characters are encoded. Each ASCII character consumes 7 bits of memory space.

In C language, character variables are declared. Character variables store values as per the requirement of the problem logic. To convey the character variable value to the compiler of C language it has to be converted into 0 or 1. Thus each ASCII code of characters given by American Standard Code for Information Interchange is used to communicate to computers. ASCII codes are converted to 0’s and 1’s which are then used to develop electronic communication.

ASCII value is a digit given to each character. In C language the character set is defined and it’s cardinality is 127. Thus, each element of this character set is assigned a digit beginning with 0 and ends with 127.

English alphabet is also assigned digits. Following is the ASCII code for english alphabets:

Table 1 ASCII Character Codes

English Alphabet

ASCII codes

Lower Case Letters

a

097

b

098

c

099

d

100

e

101

f

102

g

103

h

104

i

105

j

106

k

107

l

108

m

109

n

110

o

111

p

112

q

113

r

114

s

115

t

116

u

117

v

118

w

119

x

120

y

121

z

122

Upper Case Letters

A

065

B

066

C

067

D

068

E

069

F

070

G

071

H

072

I

073

J

074

K

075

L

076

M

077

N

078

O

079

P

080

Q

081

R

082

S

083

T

084

U

085

V

086

W

087

X

088

Y

089

Z

090

If Table 1 is analysed closely then an implicit logic is projected. Logic that is projected is that the difference between Lowercase letter and Uppercase letter is 32. For example, Lower case letter ‘a’ has ASCII code 97 and Uppercase letter ‘A’ has ASCII code 65. The difference between the two ASCII codes is 97 – 65 = 32. Thus if 32 is added to ASCII code of Uppercase letter ‘A’ then we get lowercase letter ‘a’ and if 32 is subtracted from ASCII code of ‘a’ we get Uppercase letter ‘A’. This logic will be used to develop a C Program to Convert Lowercase to Uppercase.

Solution To Problem

Following is the program to convert lowercase letters to uppercase letters.

#include <stdio.h>
#include<string.h>

int main( )
{
    char  L_U[100], U[100];
    int ii, count;
    printf(" \n Enter a string ");
    scanf("%[^\n]", L_U);
    count = strlen(L_U);
    for(ii=0; ii<count; ii++)
    {
       if(L_U[ii] >= 'a' && L_U[ii] <= 'z')
       {
             U[ii] = (char) (L_U[ii] - 'a'+'A');
       }
       else
       {
            U[ii] = L_U[ii];
       }
    }
    printf("\n");
    for(ii=0; ii<count; ii++)
    {
        printf("%c", U[ii]);
    } 
    return 0;
}
Output:

Enter a string This is boy

THIS IS BOY
Code Analysis

In this code two character arrays are declared: L_U[100] and U[100] along with two integer variables ii and count. Following is the code:

                                   char  L_U[100], U[100];
                                   int ii, count;

Input is taken from the user in the character array L_U.

Length of the character string input by user is calculated using the inbuilt string function of C language called strlen( ). Following is the code to find length of the string:

                                    count = strlen(L_U);

Length of the string is stored in integer variable count.

Since the program is to convert lowercase letter to uppercase letter, each character is picked from the input string of the user and ASCII value of lower case letter is subtracted and ASCII value of Uppercase letter is added to it. This is done by following code:

                                    U[ii] = (char) (L_U[ii] - 'a'+'A');

This code is executed in a for loop and the terminating condition of the loop is the length of the string.

Conversion result is stored in character array U[100].

Value of the character array is displayed using a for loop. This is the code for this:

                                    for(ii=0; ii<count; ii++)
                                    {
                                          printf("%c", U[ii]);
                                    } 

Conclusion

Objective was to develop a C program to convert lowercase letters to uppercase letters. To achieve this objective understanding of the ASCII encoding of the character set is necessary. Understanding of the ASCII character set is developed in the problem description section.

Problem analysis section provides a brief description of the lowercase letter and uppercase letters. Based on the understanding developed in the problem analysis and problem description section program “C Program to Convert Lowercase to Uppercase” is developed in the Solution to Problem section.

Analysis of the code is done in the Code Analysis Section. Code analysis provides you with the internal working of the code. Snapshot of the code window and output window is given to make learner familiar with the programming interface.