C Program to Convert Lowercase Character to Uppercase Character

Here we will discuss how to convert lowercase to uppercase in C? How to convert lowercase to uppercase in c without using string function?

For uppercase to lowercase see here:- Uppercase to Lowercase in C | C Program to Convert Uppercase to Lowercase without using string function

There are many different ways to write a C program to convert lowercase to uppercase of a string. In this post, we will develop C program to convert lowercase character to uppercase character in 3 ways,

1) Using strupr() pre-defined function of “string.h” header file
2) Without using any string manipulation library functions
3) Using toupper() pre-defined function of “ctype.h” header file

The strupr() function

To Convert All lowercase into uppercase in a string, one function strupr() is defined under the standard library file string.h. It converts all lowercase characters into uppercase characters in a string. The strupr() function converts characters based on the ASCII character system. We can do the same work without using any function also.

If we pass the computer then it becomes COMPUTER. The strupr() function converts only uppercase character, not disturb any other characters. For example:- a+Pn21 will be converted as A+PN21 (Only lower case a & n changed to Uppercase Character A & N, nothing happens with remaining characters).

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

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

   // display output
   printf("In Upper Case:\n");
   puts(strupr(s));

   return 0;
 }

Output:-

Enter a string: know programming
In Upper Case:
KNOW PROGRAMMING


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

The value of a in ASCII is 97, if we subtract 32 it becomes 65 which is ASCII value of A.
Similarly,

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

So, All lowercase characters ASCII value is from 97 to 122 and if we subtract 32 in each lowercase character only then it will become uppercase character.

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

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

     // iterate loop until
     // the end of the string
     while( s[i] != '\0' ) 
     {
        // if character is in lowercase
        // then subtract 32 
        if( s[i] >= 'a' && s[i] <= 'z' )
        {
           s[i] = s[i] - 32;
        }

        // increase iterator variable
        i++;
     }

     // display result
     printf("In Upper Case is: "); 
     puts(s);

     return 0;
 }

Output:-

Enter a string: computer2020
In Upper Case is: COMPUTER2020

In this program in place of ‘a’ we can also use 97 directly which is ASCII value of character ‘a’. Similarly can be done with character ‘z’ also.

if(s[i] >= 97 && s[i] <= 122)
{
    s[i] = s[i] - 32;
}

Using the pre-defined function toupper()

Using toupper() function we can convert uppercase character to lowercase character. This function works on character, and it is defined in “ctype.h” The toupper() function also converts only lowercase characters to uppercase characters. It doesn’t disturb the remaining characters.

Difference between strupr() and toupper():- The strupr() function converts all lowercase characters of a string to the uppercase character at a time. But the toupper() function works on character, not on a string. It works only one character at a time. If the given character is in lowercase then the toupper() function converts that character to uppercase.

Working with toupper() need a loop to iterate the characters of the string but strupr() function does this task itself.

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

   // take input
   printf("Enter a lower-case String: ");
   scanf("%[^\n]", str);

   // loop to iterate the characters
   // of the string
   for(int i=0; str[i]!='\0'; i++)
   {
      // convert lowercase character
      str[i] = toupper(str[i]);
   }

   // display result
   printf("Upper case string is: %s\n", str);

   return 0;
}

Output:-

Enter a lower-case String: c programming
Upper case string is: C PROGRAMMING

The strupr() converts all lowercase characters of a string to uppercase characters. We need to use only one strupr() function in the C program to convert lowercase to uppercase characters of a string.

The toupper() converts only one lowercase character at a time. So, we need to use a loop with toupper() function in the C program to convert lowercase to uppercase characters of a string.

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!

2 thoughts on “C Program to Convert Lowercase Character to Uppercase Character”

  1. Praveen Kumar Khandelwal

    I like the quiz … It’s nice to now basic concepts pls send me such quiz so that I can improve my knowledge in c program …

Leave a Reply