C Program to convert uppercase string to lowercase string

This blog post explains how you can write a C Program to convert uppercase string to lowercase string. First, we will see that how we can convert the lowercase string to the uppercase string without using the library function, then later we will take the help of the tolower function to convert the uppercase string to a lowercase string.

Example:

Input string: I Love to Read AticleWorld.COM.


Output
Lowercase string: i love to read aticleworld.com.

 

 

C Program to convert uppercase string to lowercase string without using the library function:

I believe you know that each “C” characters have an ASCII value. The ASCII (American Standard Code for Information Interchange) is a character encoding standard for electronic communication.

So whenever you write ‘A’, it is internally converted to its ASCII value which is 65. ASCII value of uppercase letters in C belongs to the range 65 to 90 and for the lowercase, it is 97 to 122. If you will add 32 in uppercase letter ASCII value it would be ASCII value of corresponding lower case. We will follow the same logic here to convert an uppercase letter in lowercase. See the below example,

'A' + 32 => 'a';

'Z' +32 => 'z'

Algorithm:

1. Traverse the given string character by character.

2.  If the character is an uppercase letter, then add 32 in it to convert its corresponding lowercase letter.

3. Using the putchar function we will print each character in the while loop and increment the index for the next character.

#include <stdio.h>

int main ()
{
    unsigned int i=0;
    char str[]="AtiClE WorlD.COM";
    while (str[i])
    {
        /*
         if(str[i]>='A' && str[i]<='Z')

         You can use this condition or
         you can use the below one with ASCII value
         which I have used.
         */
        if(str[i] >= 65 && str[i] <= 90)
        {
            str[i]+= 32;
        }
        putchar ((unsigned char)str[i]);
        i++;
    }
    return 0;
}

Output:

aticle world.com

 

C Program To Convert Uppercase String To Lowercase Using the tolower:

Algorithm:

1. Traverse the given string character by character and passed it into the tolower function.

2. The tolower function converts the uppercase letter to a corresponding lowercase letter and left another letter unchanged.

3. Using the putchar function we will print the return value of the tolower function.

#include <stdio.h>
#include <ctype.h>

int main ()
{
    unsigned int i=0;
    char str[]="AtiClE WorlD.COM";
    
    while (str[i])
    {
        putchar (tolower((unsigned char)str[i]));
        i++;
    }
    
    return 0;
}

Output:

aticle world.com

 

 

C Program To Convert Uppercase String To Lowercase Using the Recursion:

 

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

#define ARRAY_SIZE 100

int converStringLowerCase(char *str)
{
    static int i=0;
    if(str[i])
    {
        if(str[i]>=65 && str[i]<=90)
        {
            str[i]+=32;
        }
        i++;
        converStringLowerCase(str);
    }
}


int main()
{
    char str[ARRAY_SIZE];

    printf("Enter a string: ");
    fgets(str,ARRAY_SIZE,stdin);

    //Call function to convert in lowercase
    converStringLowerCase(str);

    printf("string in lowercase =%s",str);

    return 0;
}

Output:

Enter a string: AmlendrA
string in lowercase =amlendra

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *