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

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

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

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
- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
- Java Program to Print Odd Numbers from Array
- Java Program to Print All Even Numbers in Array
- Java Program to Find the Sum of Array Elements
- Java Program to Delete Element of Array At Given Location
- Java Program to Delete a given Element of Array
- Java Program to Delete Element at End of Array
- Java Program to Insert Element in Array at given Location
- Java Program to Insert Element At the End of Array
- Java Program to Print Length of an Array
- Reverse Array without using Second Array or inplace Reversal Java Program
- Java Program to Print Array in Reverse Order
- Java Program to Sort String Character in Descending order
- Java Program to Sort String in Ascending Order
- Java Program to Print non Repeating Characters in String
- Java Program to Find Sum of Integers in the String
- Java Program to Remove Duplicates From String
- Java Program to Concatenate two Strings
- Java Program to Check if two Strings are Same
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions