Convert Uppercase to Lowercase in C

Here we will discuss how to convert uppercase to lowercase character in C? C program to convert uppercase to lowercase using string function. We will also write C program to convert uppercase to lowercase without using string function.

Write a C Program to convert uppercase into lowercase. To convert all uppercase into lowercase one function strlwr() is defined under the standard library file. The strlwr() converts all Uppercase characters into lowercase in a string. It converts based on the ASCII character system. If We Pass COMPUTER then it becomes computer.

The strwlr() function converts only the Upper case character. It does not disturb any other characters. For example:- A+nN* Becomes a+nn* (Only uppercase character A & N changed to lower case a & n, nothing happens with remaining characters).

#include<stdio.h>
#include<string.h>
int main()
{
   char s[100];

   printf("Enter a string: ");
   scanf("%[^\n]",s);

   printf("In Lower Case:\n");
   puts(strlwr(s));

   return 0;
 }

Output:-

Enter a string: I Love Programming
In Lower Case:
i love programming

C program to convert uppercase to lowercase without using string manipulation library functions

We can write a C program to convert uppercase to lowercase without using any string manipulation library functions.

The value of A in ASCII is 65, add +32 it becomes 97 which is ASCII value of a.
Similarly,

B = 66 + 32 = 98 = b
C = 67 + 32 = 99 = c
D = 68 + 32 = 100 = d
So on,
Z = 90 + 32 = 122 = z

So, All uppercase characters ASCII value is from 66 to 90 and if we add +32 in each uppercase character then it will become the lowercase character.

#include<stdio.h>
int main()
{
   char s[100];
   int i=0;
   printf("Enter a string: ");
   scanf("%[^\n]",s);

   while(s[i]!='\0')
   {
      if(s[i]>='A' && s[i]<='Z') s[i] += 32;
      i++;
   }

   printf("In Lower Case is: ");
   puts(s);

   return 0;
 }

Output:-

Enter a string: 2019 Know Program
In Lower Case is: 2019 know program

Using tolower() predefined function

Using the tolower() function we can convert lowercase characters to uppercase characters. This function works on a character, and it is defined in “ctype.h

#include<stdio.h>
#include<ctype.h>
int main()
{
   char str[100];
   printf("Enter an Upper-case String: ");
   scanf("%[^\n]",str);

   for(int i=0; str[i]!='\0'; i++)
   {
      str[i]=tolower(str[i]);
   }
   printf("Lower case string is: %s\n", str);

   return 0;
}

Output:-

Enter an Upper-case String: KNOW PROGRAM
Lower case string is: know program

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

More C programming Examples on String

1 thought on “Convert Uppercase to Lowercase in C”

  1. As i recall tolower only applies to ASCII characters. isascii should be used to verify it is ascii before applying the tolower.

Leave a Reply