C Program to convert in uppar case
Objective
Write a C program to manually convert a lowercase string into uppercase.
Algorithm / Approach
- Read a string into array
a. - Loop through the string until the null terminator.
- Check if the current character is a lowercase letter:
if(a[i] >= 'a' && a[i] <= 'z'). - Convert it to uppercase by subtracting 32 from its ASCII value:
a[i] = a[i] - 32. - Print the modified string.
main.c
#include<stdio.h>
int main( ) {
char a[100],i=0;
printf("Enter a string : ");
gets(a);
while (a[i] != '\0') {
if (a[i] >= 'a' && a[i] <= 'z') {
a[i] = a[i] - 32;
}
i++;
}
printf("After Conversion: %s", a);
return 0;
}
Expected Output
Enter a string to convert in upper case : prowess After Conversion: PROWESS
Explanation of the Program
- This program takes advantage of how characters are stored as ASCII integers in C.
- In the ASCII table, lowercase letters start at 97 ('a') and uppercase letters start at 65 ('A'). Because the alphabets are sequentially aligned, the exact numerical difference between any lowercase letter and its uppercase equivalent is always 32 (97 - 65 = 32).
Complexity
Time Complexity
O(n)
Space Complexity
O(1) - Modified in place.