Select Page

Converting string uppercase to lowercase

converting string uppercase to lower-in this tutorial – we will discuss how to convert uppercase into the lowercase string using the built-in method and without using the Built-in method in C programming language-C language provides us the inbuilt method tolower() to change an uppercase to the lowercase string.

 

Using tolower()

int main()
{
   char st[]="MANAS HALDER";
   char ch; 
   for(int i=0; st[i]!='\0'; i++)
    {
      ch=st[i];
      printf("%s"(tolower(ch));
    }
   return 0;
}

Now we will see that without using a built-in method how to convert uppercase string to lowercase string?

Before converting string uppercase to lowercase we need to know about the ASCII code. An ASCII code is the method of representing characters as an integer, which is a 7-bit representation.

For uppercase letters that is A, B, C, D …… X, Y, Z, and the ASCII values lie in the range of 65,66,67……89,90.

For lowercase letters that are for a,b,c,d…..x,y,z, the ASCII values lie in the range of 97,98,99,…….122.

Logic is that we check the ASCII code of the character if the ASCII code lies between 65 to 90 then add 32 so that it will automatically be converted from uppercase to lowercase string.

 

Source Code

int main(){   char str[25];   int i;   printf("\nEnter the string to change the case");   gets(str);   for(i=0; str[i]!='\0'; i++)   {      if(str[i]>='A'&& str[i]<='Z')          str[i]=str[i]+32;       }   printf("\nNew string is=%s", str);}

Output

As we mentioned before all the upper case characters [A to Z] ASCII value ranging from 65 to 90 and lower case character [a to z] ASCII value ranging from 97 to 122. So the following program we are going to use direct  ASCII value instead of using characater.

int main()
{
   char st[25];
   int i,len;
   printf("\n\n\tEnter the string");
    gets(st);
   len=strlen(st);
   for(i=0; i<len; i++)
    {
      if(st[i]>=65 && st[i]<=95)
           st[i]=st[i]+32;
     }
    printf("\n\n\tNew String is=%s",st);
}

output