C program to convert lowercase char to uppercase of string

In this tutorial we are going to learn how to write a program in C to convert a lowercase string to uppercase.

There are multiple ways to write the program in C for the above problem. Here we will see 3 ways to do this.

In this tutorial we will learn writing the C program to convert all lowercase character to uppercase character of String. To perform this task first we will identify the lowercase character. If we find the lowercase then convert this lowercase to uppercase.

1) Without using string manipulation library functions

Here we will use the ASCII value of the character to convert lower case value to upper case value. The ASCII value of a is 97 and z is 122.

As we all know that ASCII value of a is 97. Now when we subtract 32 it will become 65 which the ASCII value of A.
Using above formula we can find upper character ASCII value.

Below is the example

ASCII value of b is 98.

Now to calculate ASCII value of B we will do

98 – 32 = 66 (It is a ASCII value of B)

Similarly for
c = 99 – 32 = 67 = C
d = 100 – 32 = 68 = D etc.

#include<stdio.h>
 int main()
 {
     char str[100];
     int i = 0;
     printf("C program to convert lower case character into upper case character of String \n");
     printf("Please enter a string: ");
     scanf("%[^\n]", str);
     while( str[i] != '\0' )
     {
        if( str[i] >= 'a' && str[i] <= 'z' )
        {
           str[i] = str[i] - 32;
        }
        i++;
     }
     printf("String after converting into upper case = %s", str);
     return 0;
 }

Output

C program to convert all lowercase character to uppercase of string

2) Using strupr() function

The strupr() is the library function available at string.h header file which converts lower case string into upper case string.

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

    printf("C program to convert lowercase into uppercase character of String \n");
    printf("Please enter a string: ");
    scanf("%[^\n]", str);
	strupr(str);
    printf("String after converting into uppercase = %s",str);
    return 0;

}

Output

C program to convert lowercase to uppercase of string

3) Using the pre-defined function toupper()

toupper() function is predefined function which is available in ctype.h library. We can convert uppercase character of any string to lowercase character using this function. toupper() function only converts lowercase characters to uppercase characters without other characters.

#include<stdio.h>
#include<ctype.h>
int main()
{
   char str[100];
   printf("C program to convert lowercase into uppercase character of String \n");
   printf("Please enter a String: ");
   scanf("%[^\n]", str);
   for(int i=0; str[i]!='\0'; i++)
   {
      str[i] = toupper(str[i]);
   }
   printf("String after converting into uppercase: %s\n", str);
   return 0;
}

Output

C program to convert all lowercase character to uppercase of string

Difference between strupr() and toupper()

  • strupr() function converts all lowercase characters of a string to the uppercase character at a time.
  • But toupper() function converts character by character. It works on one character at a time and convert it into uppercase if it is in lowercase.

Share on

Also Prepare Below Important Question

Interview Questions Categories

C Programming Interview Preparation

Core Java Programming Interview Preparation

Python Programming Interview Preparation