C Programming

C Uppercase to Lowercase

This article will go through numerous techniques of how to use the C language to transform uppercase letters (characters or strings) into lowercase letters. The capital letter of the word would be the uppercase characters. Likewise, the small letter of the word represents a lowercase character. While we utilize ASCII values to transform capital characters or strings to lowercase, the procedure transforms uppercase to lowercase strings in C language. Uppercase characters (A -Z) contain ASCII values 65 to 90, while lowercase characters (a -z) possess ASCII values in the range 97 to 122.

Use the tolower() Method to Transform Uppercase Characters into Lowercase Characters

To transform uppercase characters into lowercase characters, we can use the tolower() method. If the tolower() method is called with a parameter that is not an uppercase character, it provides the same text that was supplied to it. It is declared in the library <ctype.h>.

In C language, the word is handled as an integer. That whenever a text is provided as a parameter, the appropriate ASCII code (number) of the word is delivered rather than the text itself.

#include<stdio.h>
#include<ctype.h>
int main()
{
   char s[100];
   printf("Enter an Upper-case String: ");
   scanf("%[^\n]",s);
   for(int j=0; s[j]!='\0'; j++)
   {
      s[j]=tolower(s[j]);
   }
   printf("Lower case string is: %s\n", s);
   return 0;
}

The step is to integrate the required libraries. #include <stdio.h> and #include <cstype.h>. Then we define the main() function. Within the body of this function, we initialize variable ‘s’ for the string. Here we specify the size and data type of the entered string. The printf() method is being called to display the line so the user has to enter any string which has an upper case character.

In the next step, we utilize the scanf() function. It is a built-in function of the C language that is used to get organized information. Then we apply for the loop. Inside for loop first we initialize the variable ‘j’. And then we set the condition for this variable. After this, we increment the value of variable ‘j’. Further, we employ the tolower() method which modifies the entered string into a lower-case string.

After converting the string, we utilize the printf() method to show the string in the lower case form. In the end, the return 0 command is applied to terminate the code.

Converting Uppercase Strings to Lowercase Strings, Use For Loop

In this step, the user will be required to input a string that contains all the uppercase characters or a few uppercase characters. Then, we will transform it to an entire lowercase string with the help of for loop in the following example:

#include<stdio.h>
#include<string.h>
int main(){
   char st[30];
   int j;
   printf("Enter the string: ");
   scanf("%s",st);
   for(j=0;j=65&&st[j]<=90)
         st[j]=st[j]+32;
   }
   printf("\nLower Case String is: %s",st);
   return 0;
}

Here, we are going to introduce the header files <stdio.h> and <string.h>. In the next step, we call the main() method. We declare a variable for the string. The data type of the string variable is character. We can set the size of the string here. Meanwhile, we initialize another variable. Now, we get a string from the user of his own choice so we apply the printf() function.

In addition to this, the scanf() method is also being called to read the entered string. Further, for loop is utilized to convert the entered string which has all uppercase characters to lowercase characters. We first set the variable ‘j’ within the loop. Within the for loop, we employ the len() function to find the length of the defined string. Moreover, we define the requirement for this variable. The value of ‘j’ must be less than or equal to the length of the string.

Just after that, we increase the value of variable ‘j’. We apply if condition here. That means the length of the string must be between 65 and 90 because the ASCII code of A is 65 and the ASCII code of Z is 90. Moreover, we append 32 to the defined string. That is how we change it to lowercase. Outside for loop, we employ the printf() function and obtain the resultant string in the lowercase form. To end the program we use the return 0 statement.

Use the strlwr() Method to Transform an Uppercase String into a Lowercase One

The strlwr() method is a string library standard method that is being used to transform an uppercase text to a lowercase text by providing the specified string as a parameter and getting the string having lowercase characters. The uppercase string is passed to the strlwr() method as a parameter, and then the strlwr() method produces the lowercase string.

Let’s see how to utilize the in-built method strlwr() in C language to change an uppercase text to a lowercase one. 

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char s[80];
    printf("Enter uppercase String: ");
    gets(s);
    printf("\nIts Lowercase = %s", strlwr(s));
    getch();
    return 0;
}

First of all, three header files <stdio.h>, <conio.h> and <string.h> are included. The main() method is then called. We declare ‘s’ for the string inside the body of this method. The size of the provided string is mentioned here. To print the statement, the printf() function is being used, and the user will have to provide any string that contains upper case words.

The gets() method is applied in the next step. It is built-in functionality of the C programming language which can be used to take the string. Following that, we have been using the strlwr() technique to update the provided string to a lower string. We just use the printf() approach to obtain the string in lower case once it has been modified. Additionally, the getch() method is applied. The return 0 statement will be utilized to end the program.

Conclusion

Different methods of converting the uppercase strings into lowercase strings have been described in this article. We have utilized for loop, strlwr(), and tolower() functions with their examples to update the strings to lowercase strings. These examples are easy to understand and implement for all novice users.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content