C program to convert string to lowercase

Write a C program to convert uppercase string to lowercase using for loop. How to convert uppercase string to lowercase without using inbuilt library function strlwr(). How to convert string to lowercase using strlwr() string function.

Example

Input

Input string: I love CODEFORWIN.

Output

Lowercase string: i love codeforwin.

Required knowledge

Basic C programming, Loop, String

Logic to convert uppercase string to lowercase

Internally characters in C are represented as an integer value known as ASCII value. Which means if we write a or any other character it is translated into a numeric value in our case it is 97 as ASCII value of a = 97.

Here what we need to do is first we need to check whether the given character is upper case alphabet or not. If it is uppercase alphabet just add 32 to it which will result in lowercase alphabet (Since ASCII value of A=65, a=97 their difference is 97-65 = 32).

Algorithm to convert uppercase to lowercase
%%Input : text {Array of characters / String}
         N {Size of the String}
Begin:
    For i ← 0 to N do
        If (text[i] >= 'A' and text[i] <= 'Z') then
            text[i] ← text[i] + 32;
        End if
    End for
End

Program to convert string to lowercase

/**
 * C program to convert string to lowercase
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE];
    int i;
 
    /* Input string from user */
    printf("Enter any string: ");
    gets(str);


    // Iterate loop till last character of string
    for(i=0; str[i]!='\0'; i++)
    {
        if(str[i]>='A' && str[i]<='Z')
        {
            str[i] = str[i] + 32;
        }
    }

    printf("Lower case string: %s", str);

    return 0;
}

You can also use inbuilt library function strlwr() defined in string.h header file to convert strings to lowercase.

Read more – Program to convert string to uppercase

Program to convert string to lowercase using strlwr() string funtion

/**
 * C program to convert string to lowercase using strlwr() string function
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE];

    /* Input string from user */
    printf("Enter any string: ");
    gets(str);

    strlwr(str); // Convert to lowercase

    printf("Lowercase string: %s", str);

    return 0;
}

Output

Enter any string: I love CODEFORWIN.
Lowercase string : i love codeforwin.

Happy coding 😉