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.
Also Prepare Below Important Question
- 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
- Python Program to Delete element at End of Array
- Python Program to Copy one String to Another String
- Python Program to Remove Repeated Character from String
- Print highest frequency Character in String in Python
- Python Program to Convert lowercase vowel to uppercase
- Convert Celsius to Fahrenheit in Python
- Leap Year Program in Python
- Python Program to convert Decimal to Octal number
- Python Program to Convert Decimal Number into Binary
- Find all Pairs Whose Sum is Equal to Given number in Python
- Python Program to Replace First Vowel With ‘-‘ in String
- Python Program to find Prime factors of given integer
- Python Program to Print Prime Number in given range
- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
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