For instance, if we have the uppercase letter 'A' and wish to transform it into the lowercase letter 'a', we simply need to increment the ASCII value of 'A' (65) by 32, resulting in the character 'a' with an ASCII value of 97 (65 + 32). In the same manner, we can change the uppercase string 'HELLO' to the lowercase string 'hello'.
Program to convert the uppercase character into lowercase
Let's develop a C program that converts uppercase characters to lowercase characters.
Program1.c
#include <stdio.h>
#include <conio.h>
int main ()
{
char upr, lwr; // declare variables
int ascii;
// convert in lower case
printf (" Enter the Upper Case Character: ");
scanf (" %c", &upr);
ascii = upr + 32;
printf (" %c character in Lower case is: %c", upr, ascii);
// convert in upper case
printf (" \n Enter the Lower Case Character: ");
scanf (" %c", &lwr);
ascii = lwr - 32;
printf (" %c character in the Upper case is: %c", lwr, ascii);
return 0;
}
Output
Enter the Upper Case Character: A
A character in Lower case is: a
Enter the Lower Case Character: z
z character in the Upper case is: Z
Program to convert the uppercase string into lower case using for loop
Let's create a basic program in C that converts uppercase characters to lowercase using a for loop.
Program2.c
#include <stdio.h>
#include <conio.h>
int main ()
{
char str[30];
int i;
printf (" Enter the string: ");
scanf (" %s", &str); // take a string
// use for loop to change string from upper case to lower case
for ( i = 0; i <= strlen (str); i++)
{
// The ASCII value of A is 65 and Z is 90
if (str[i] >= 65 && str[i] <= 90)
str[i] = str[i] + 32; /* add 32 to string character to convert into lower case. */
}
printf (" \n Upper Case to Lower case string is: %s", str);
return 0;
}
Output
Enter the string: WELCOME
Upper Case to Lower case string is: welcome
Program to convert the uppercase string into lower case using while loop
Let's examine a scenario where we aim to display the lowercase string derived from an uppercase string using a while loop in the C programming language.
Program3.c
#include <stdio.h>
#include <conio.h>
int main ()
{
char upr[50], lwr[50]; // declare character array
int i = 0;
printf (" Enter an upper case string: ");
gets (upr); // use gets() function to take string
/* use while loop to iterate the character array string until '\0' not occur. */
while (upr [i] != '\0')
{
lwr [i] = upr[i] + 32; // Add 32 to string character to convert into lower case. i++;
}
printf (" \n The lowercase string is: %s", lwr);
return 0;
}
Output
Enter an upper case string: CPROGRAMMING
The lowercase string is: cprogramming
Program to convert the uppercase string into lowercase using the strlwr function
The strlwr function, found within the string library, is a built-in function designed to transform uppercase strings into lowercase. This is achieved by providing the desired string as an argument, resulting in the return of the lowercase version of the input string.
Syntax
strlwr (upr);
The strlwr function includes an upr parameter denoting the uppercase string, while the strlwr function utilizes the upr parameter to yield a lowercase string.
Let's develop a script to transform uppercase text to lowercase by leveraging the pre-existing strlwr function within C programming.
Program4.c
#include <stdio.h>
#include <conio.h>
int main ()
{
char upr[50]; // declare character array
printf (" Enter the upper case string: ");
gets (upr); // use gets() function to take string
// use strlwr() function to change upper case into lower string
printf (" \n The lowercase string is: %s", strlwr(upr));
return 0;
}
Output
Enter the upper case string: GOOD BYE
The lowercase string is: good bye
Program to convert the uppercase string into lowercase using the user-defined function
Let's explore an illustration demonstrating the transformation of uppercase characters to lowercase ones by utilizing a custom function in the C programming language.
Program5.c
#include <stdio.h>
#include <conio.h>
// create a function
void upr_lwr (char *str)
{
int i;
// use for loop to iterate the length of the string
for ( i = 0; str[i]; i++)
{
if ( str [i] >= 65 && str [i] <= 90)
str[i] = str[i] + 32; /* add 32 to string character to change into lowercase */
}
}
int main ()
{
char str[100]; // declare the size of character array
printf (" Enter the upper case string: ");
gets (str); // use gets() function to take string
// call upr_lwr() function
upr_lwr (str);
printf (" \n The lowercase string is: %s", str);
return 0;
}
Output
Enter the upper case string: LOGIC PRACTICE
The lowercase string is: logic practice
Program to convert the uppercase string into lowercase using the recursion function
Let's explore an illustration demonstrating the utilization of a recursive function in C programming to output the lowercase string from an uppercase string.
Program6.c
#include <stdio.h>
#include <conio.h>
#include <string.h>
int upr_lwr (char *str)
{
static int i = 0;
if (str[i])
{
/* If statement check the enter character's ASCII value is more than 65 and less than equal to 90. */
if ( str[i] >= 65 && str[i] <= 90)
str[i] = str[i] + 32; // add 32 to string character to change into lowercase
i++;
upr_lwr(str);
}
}
int main ()
{
char str[100]; // declare the size of character array
printf (" Enter the upper case string: ");
gets (str); // use gets() function to take string
// call upr_lwr() function
upr_lwr (str);
printf (" \n The lowercase string is: %s", str);
}
Output
Enter the upper case string: C PROGRAMMING LANGUAGE
The lowercase string is: c programming language