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.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…