codescracker
c

C Program to Convert Uppercase to Lowercase



« Previous Program Next Program »

In this article, you will learn and get about converting a given character or string from uppercase to lowercase. Here are the list of programs that are available over here:

But before going to these programs, let's first discuss about some main topics involved in these programs.

Uppercase Character

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

Uppercase String

If each and every character of a string are written in capital letter, then that string can be called as uppercase string. For example, CODESCRACKER

Lowercase Character

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

Lowercase String

If each and every character of a string are written in small letter, then that string can be called as lowercase string. For example, codescracker

Uppercase to Lowercase Conversion Formula

The formula to convert uppercase character to lowercase is

lowerChar = upperChar + 32

Because the ASCII values of A-Z are 65-90 and a-z are 97-122. That is, the ASCII value of A (capital A) is 65, whereas the ASCII value of a (small a) is 97. And the difference is 32. The difference is in a way that, the ASCII value of lowercase a is 32 greater than the ASCII value of uppercase A. So to convert from uppercase character to lowercase, we have added 32 to the upperChar variable (holds uppercase character). Now let's move on to the program.

Uppercase Character to Lowercase in C

The question is, write a program in C that converts any given character (in uppercase) from user (at run-time) to lowercase character. The answer to this question is given below.

// Convert Uppercase Character to Lowercase in C
// -----------codescracker.com-----------

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

This program is compiled and executed under Code::Blocks IDE. Here is the sample run:

c program convert uppercase character to lowercase

Now supply any character in uppercase (capital letter) say C and press ENTER key to see the following output:

uppercase to lowercase c

If user enters C as uppercase character input, then C gets initialized to upperChar variable. And the statement:

ascii = upperChar;

means the ASCII value of C, that is 67 gets initialized to ascii variable. And the statement:

lowerChar = ascii+32;

means ascii+32 or 67+32 (on putting the value of ascii) or 99 gets initialized to lowerChar variable. And while printing the lowerChar as a character, we have used %c format specifier that prints the character whose ASCII value is 99. That is C.

Convert Uppercase String to Lowercase in C

Now let's convert string from uppercase to lowercase. To convert uppercase to lowercase string, you have to ask from user to enter the string (in uppercase) to convert it into lowercase. To change uppercase string into lowercase string, just add 32 in each and every character of the given string as shown in the following program.

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

// Convert Uppercase String to Lowercase in C
// -----------codescracker.com-----------

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

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

uppercase to lowercase conversion in c

Uppercase String to Lowercase in C using Library Function

The last program of this article uses the standard library function strlwr() that takes string as argument and returns the same string in lowercase form. Let's have a look at the program given below:

// Convert Uppercase String to Lowercase in C using library Function
// -----------codescracker.com-----------

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

This program will produce the same output as of previous one. To learn more about function, refer to Function in C tutorial.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker