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("Convert lower case into upper case Char using C \n");
printf("Please enter a string: ");
fgets(str, 100, stdin);
while( str[i] != '\0' )
{
if( str[i] >= 'a' && str[i] <= 'z' )
{
str[i] = str[i] - 32;
}
i++;
}
printf("String after converting into upper case : \n%s", str);
return 0;
}
Output
Convert lower case into upper case Char using C
Please enter a string: Quescol Website
String after converting into upper case :
QUESCOL WEBSITE
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("Convert lower case into upper case Char using C \n");
printf("Please enter a string: ");
fgets(str, 100, stdin);
strupr(str);
printf("String after converting into uppercase = %s",str);
return 0;
}
Output
Convert lower case into upper case Char using C
Please enter a string: Quescol Website
String after converting into upper case :
QUESCOL WEBSITE
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("Convert lower case into upper case Char using C \n");
printf("Please enter a String: ");
fgets(str, 100, stdin);
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
Convert lower case into upper case Char using C
Please enter a String: Quescol Education
String after converting into uppercase: QUESCOL EDUCATION
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.
[wpusb]
Also Prepare Below Important Question
- Sum of digits of Given Number in Java
- Hibernate Interview Questions for 2+ years of experience
- 68 Most Important Microservices Interview Questions
- 60 Most Important Git Interview Questions
- 50+ Mostly asked Java Interview Questions for 6 Years Exp.
- 60+ Mostly Asked Java Interview Questions for 4 Years Exp
- 60+ Mostly asked Java Interview Question for 3+ Years
- 60+ Spring Boot interview questions for 4+ years Exp.
- 60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
- Scenario Based Java 8 Coding Interview Questions (For Experienced)
- Python Program to add two numbers without addition operator
- Mostly Asked Java Interview Questions For 2 Yrs Experience
- Find All Pairs in Array whose Sum is Equal to given number Using Java
- Java Program to find GCD of two Numbers using Recursion
- Python Program to Separate Characters in a Given String
- Python Program to add two number using Recursion
- Python Program to Find Highest Frequency Element in Array
- Python Program to Merge two Arrays
- Perform left rotation by two positions in Array Using Python
- Python Program to Delete Element at Given Index in Array
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