codescracker
c

C Program to Convert Lowercase to Uppercase



« Previous Program Next Program »

In this article, you will learn and get code about converting any character or string from lowercase to uppercase. Here are the list of programs you will learn about:

But before going to these program, let's first understand about lowercase and uppercase character or string.

Lowercase Character

A character written in small letter is known as lowercase character. For example, c

Lowercase String

If each and every character of a string written in small letter is known as lowercase string. For example, codescracker

Uppercase Character

A character written in capital letter is known as uppercase character. For example, C

Uppercase String

If each and every character of a string, written in capital letter is known as uppercase string. For example, CODESCRACKER

ASCII Value of all Alphabets

ASCII values of A-Z (uppercase alphabets) are 65-90. That is, ASCII value of

And ASCII values of a-z (lowercase alphabets) are 97-122. That is, ASCII value of

Lowercase to Uppercase Formula

As you can see the ASCII values of lowercase and uppercase alphabets as given above. The ASCII value of lowercase alphabet say a is 32 greater than the ASCII value of uppercase alphabet say A. So to convert lowercase to uppercase, use the formula

upperChar = lowerChar-32

Where lowerChar is the character entered by user (in lowercase). For example, if lowerChar holds a, then by applying the above formula

upperChar = lowerChar-32
=a-32
=97-32
=65

And 65 is the ASCII value of A. Therefore, the given lowercase character a gets converted into uppercase character A. To apply the conversion for string, then convert each and every character using the above formula. Now let's move on to the program given below:

Lowercase to Uppercase Character in C

Let's first create a program that converts lowercase character to uppercase character. So the question is, write a program in C, that asks from user to enter a character (in lowercase) and convert it into uppercase. The answer to this question is given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char lowerChar, upperChar;
    int ascii;
    printf("Enter a lowercase Character: ");
    scanf("%c", &lowerChar);
    ascii = lowerChar;
    upperChar = ascii-32;
    printf("\nIts Uppercase = %c", upperChar);
    getch();
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is the sample run:

lowercase to uppercase character c

Now supply any character (in lowercase) as input say c and press ENTER key to see the following output:

c lowercase to uppercase character

Lowercase String to Uppercase in C

The question is, write a program in C that converts any given string from lowercase to uppercase. The answer to this question is given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char lowerString[50]="", upperString[50]="";
    int i=0;
    printf("Enter a lowercase String: ");
    gets(lowerString);
    while(lowerString[i]!='\0')
    {
        upperString[i] = lowerString[i]-32;
        i++;
    }
    printf("\nIts Uppercase = %s", upperString);
    getch();
    return 0;
}

The snapshot given below shows the sample run of this program:

c program convert lowercase to uppercase

As you can see from the above program, each and every character gets converted into its equivalent value in uppercase inside the while loop. After converting, just print the value of upperString as output. That's it. To learn more about while loop, then refer to while loop in C tutorial.

Lowercase to Uppercase String in C using Library Function

This is the last program of this article. This program uses standard library function strupr() that takes string as argument and returns the string given in argument to its uppercase form. Let's have a look at the program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[100];
    printf("Enter lowercase String: ");
    gets(str);
    printf("\nIts Uppercase = %s", strupr(str));
    getch();
    return 0;
}

The output produced by this program is same as of previous program.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker