How To Convert Lowercase To Uppercase In C

Lowercase characters are not preferred in professional data inputs. Many times form filling is also done in uppercase characters only or lowercase characters are converted into online forms to store the user data. Uppercase also helps in sorting the data with priority as uppercase characters are sorted before lowercase characters.

Also, a mix of lowercase characters and uppercase characters creates confusion and can be more complex to readability compared to uppercase characters.  Also, many lowercase words are defined as keywords in programming languages and database query languages as a keyword. Some examples are the final keyword, this keyword, select / update query, etc. All these keywords are pre-defined as lowercase characters and cannot be used. But, uppercase characters of the same words  differentiate them from keywords.

In C language conversion of lowercase to uppercase can be achieved by three approaches. One can be using ASCII values, another one by using string functions, and the last one by using the recursion method. All these approaches are well explained with detailed code and explanation below.

Why Is There A Need To Convert Lowercase Into Uppercase?

Converting lowercase characters to uppercase characters can help improve the consistency, readability, and accuracy of your text.  Check-in details here:

  • Readability: Uppercase characters are easy to read and more comfortable comparing lowercase characters or a mix of both. Uppercase characters are marked as important words in reading.
  • Sorting: Uppercase characters are sorted before lowercase characters. Hence it is easy to find uppercase characters.
  • Tolerate confusion: Confusion created by lowercase characters is tolerated and collision with technical language keywords is avoided by uppercase characters.
  • Screen presentation: Uppercase characters are displayed and printed on the screen with a better look and are easy to identify.
  • Important data: Uppercase characters are generally used to define important data like a person’s name and other related information.

The following section explains how to convert lowercase to uppercase in C language using sample code, output, and code explanation.

Different Approaches To Convert Lowercase To Uppercase In Java

There are three ways to convert lowercase characters to uppercase characters in C language. All of these methods can convert lowercase to uppercase letters. Here are some typical approaches:

  1. Using toupper() method
  2. Using ASCII values
  3. Using the recursion function

The programmer can achieve the conversion of lowercase to uppercase conversion using the above methods. To learn more about the approaches mentioned above and to better understand “how to convert lowercase to uppercase in C language” an explanation of methods, sample code, and their output explanations are provided below.

Approach 1 : Convert lowercase to uppercase using the toupper() method

toupper() function is the in-build function in the c language which converts the string into uppercase. This built-in function is a simple and straightforward way to convert the string. This function is pre-defined in <string.h> header files.

Sample code :  

// declare the header files
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {
    // create a char type variable
   char str[100];

   // take the user input
   printf("Enter a string: ");
   gets(str);

   // use the function with loop concept
   for(int i = 0; i < strlen(str); i++) {
      str[i] = toupper(str[i]);
   }

   // print the output
   printf("Uppercase string: %s", str);

   return 0;
}

Output:

Enter a string: school
Uppercase string: SCHOOL

Code explanation:

  1. Declare the header files for using the function from the predefined library.
  2. Take the user input.
  3. Create the loop and use toupper() to convert the input into lowercase.
  4. Print the output on the console.

Approach 2 : Convert to uppercase using ASCII values

ASCII values are the values associated with each character and symbol by the international body to make the device communication more efficient. These values can be swapped out to convert lowercase characters to uppercase.

Here is an explanation of the steps and methods used in this approach. To obtain the required result, the programmer must write a program that subtracts the ASCII value of character ‘a’ (97) from the ASCII value of the lowercase character taken from the user and adds the ASCII value of character ‘A’ (65).

Sample code:

// declare the header files
#include <stdio.h>
#include <string.h>

int main() {
   // create a char type variable
   char str[100];

   // take the user input
   printf("Enter a string: ");
   gets(str);

   // use the ASCII value concept with loop concept
   for(int i = 0; i < strlen(str); i++) {
      if(str[i] >= 'a' && str[i] <= 'z') {
         str[i] = str[i] - 32;
      }
   }

   // print the output
   printf("Uppercase string: %s", str);

   return 0;
}

Output:

Enter a string: computer
Uppercase string: COMPUTER

Code explanation:

  1. Declare the header files for using the function from the predefined library.
  2. Take the user input.
  3. Create the loop and use the ASCII value concept to convert the input into lowercase. Here, (ASCII value of ‘a’ ) 97- (ASCII value of ‘A’ ) 65= 32
  4. Print the output on the console.

Approach 3 : Create uppercase data using the recursion method

Recursion means to call the function itself till the process is not completed. In this method the concept of ASCII value is also applied. This method is difficult and not recommended at all. Sometimes, the programmer will feel that this is an irrelevant method, but still, he or she can achieve the conversion through this approach.

Sample code:

// declare the header files
#include <stdio.h>
#include <string.h>

void convertToUpper(char str[], int len) {
   // create a recursive function
   if(len == 0) {
      return; // base case
   }
   else {
      convertToUpper(str, len-1); // recursive call
      if(str[len-1] >= 'a' && str[len-1] <= 'z') {
         str[len-1] = str[len-1] - 32; // convert to uppercase
      }
   }
}

int main() {
   char str[100];

   // take the user input
   printf("Enter a string: ");
   gets(str);

   // call the recursive function
   convertToUpper(str, strlen(str)); 

    // print the output
   printf("Uppercase string: %s", str);

   return 0;
}

Output:

Enter a string: c language
Uppercase string: C LANGUAGE

Code explanation:

  1. Declare the header files for using the function from the predefined library.
  2. Create a function using the ASCII value concept for the conversion
  3. Take the user input.
  4. Print the output on the console.

Best Approach Out Of Three For Converting Lowercase To Uppercase In C Language

The toupper() method is the best approach to convert lowercase characters into uppercase characters. Here are reasons to justify the statement.

Predefined built-in method: The toupper() method is a built-in method provided by the <string.h> header file. This method is more convenient for programmers as they have to use the method directly in their code.

Less line of codes: The toupper() method is a simple and concise way to convert lowercase characters to uppercase characters. It only requires a loop to iterate the whole string, which makes it easy to use and understand.

Efficient: The toupper() method is highly optimized and efficient as it is pre-defined in the programming language. It uses advanced algorithms to perform the conversion quickly and accurately, making it suitable for use in performance-critical applications.

Beginner-friendly nature: This method is beginner friendly as it is simple and the syntax is also not that complex. It can be easily used in the conversion. It will absolutely answer the question of the beginner –  “How to convert lowercase to uppercase in C”

All three methods can achieve the same result, but using the toupper() method is the preferred method due to its simplicity, efficiency, and integrated nature.

Ultimately, the approach chosen will be determined by the specific constraints and goals of the application and the preferences and skills of the developer.

Sample Problems

Sample Problem 1

How to change lowercase to uppercase in C imagining you have to develop an application for a super-store billing counter. Write a program to take the name of the customer as the input and convert it into an uppercase to display the name on the bill.

Sample code :

// declare the header files
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {
    // create a char type variable
   char str[100];

   // take the user input
   printf("Enter a customer name: ");
   gets(str);

   // use the function with loop concept
   for(int i = 0; i < strlen(str); i++) {
      str[i] = toupper(str[i]);
   }

   // print the output
   printf("Customer name is: %s", str);

   return 0;
}

Output :

Enter a customer name: vijay
Customer name is: VIJAY

Code explanation :

  1. Declare the header files for using the function from the predefined library.
  2. Get the customer name while making the bill.
  3. Create the loop and use toupper() to convert the input into lowercase.
  4. Print the name on the bill.

Sample Problem 2

How to convert from lowercase to uppercase in C considering you have to develop an application for a school. Write a program to take the name of the teacher as the input and convert it into an uppercase to display the name on the staff information database.

Sample code :

// declare the header files
#include <stdio.h>
#include <string.h>

int main() {
   // create a char type variable
   char str[100];

   // take the user input
   printf("Enter a teacher name: ");
   gets(str);

   // use the ASCII value concept with loop concept
   for(int i = 0; i < strlen(str); i++) {
      if(str[i] >= 'a' && str[i] <= 'z') {
         str[i] = str[i] - 32;
      }
   }

   // print the output
   printf("Teacher name is: %s", str);

   return 0;
}

Output :

Enter a teacher name: deepak gunsariya
Teacher name is: DEEPAK GUNSARIYA

Code explanation :

  1. Declare the header files for using the function from the predefined library.
  2. Enter the teacher’s name.
  3. Create the loop and use the ASCII value concept to convert the input into lowercase. Here, (ASCII value of ‘a’ ) 97- (ASCII value of ‘A’ ) 65= 32
  4. Save the teacher name on the database.

Sample Problem 3

How to change from lowercase to uppercase in C using the recursion method as you have to develop an online form for the survey. Write a program to take the name of the participant as the input and convert it into an uppercase to display the name on the bill.

Sample code :

// declare the header files
#include <stdio.h>
#include <string.h>

void convertToUpper(char str[], int len) {
   // create a recursive function
   if(len == 0) {
      return; // base case
   }
   else {
      convertToUpper(str, len-1); // recursive call
      if(str[len-1] >= 'a' && str[len-1] <= 'z') {
         str[len-1] = str[len-1] - 32; // convert to uppercase
      }
   }
}

int main() {
   char str[100];

   // take the user input
   printf("Enter a participant name: ");
   gets(str);

   // call the recursive function
   convertToUpper(str, strlen(str)); 

    // print the output
   printf("Participant name is: %s", str);

   return 0;
}

Output :

Enter a participant name: rohit patil
Participant name is: ROHIT PATIL

Code explanation :

  1. Declare the header files for using the function from the predefined library.
  2. Create a function using the ASCII value concept for the conversion
  3. Enter the customer’s name.
  4. Print the name on the bill.

Conclusion

Converting uppercase characters to lowercase characters can be an essential task for application development. It helps in displaying important data in a specific format that catches the eyes of the user. Uppercase is used to specify the nouns and other data. They increase the readability of the text.

They also differentiate the keywords for programming uses. Uppercase characters give data priority while the data sorting. There can be many more reasons according to the situation.

The toupper() method is the best approach to convert lowercase characters into uppercase characters as it is simple to use and saves many lines of code. It is also easy to understand for beginners and conversion is done perfectly.

How to convert lowercase to uppercase in C is already answered properly and deeply using suitable examples and the best explanation for the beginner.