How to convert lowercase to uppercase in c programming?
Its very easy.
There are different ways to convert string from lower case to upper case and vice versa.
We will discuss following ways to convert lowercase to uppercase in c.
Lets see them one by one.
c program to convert lowercase character to uppercase character are as below
1 Using Loops to convert lowercase character to uppercase character
Characters in C programming is represented by ASCII numbers.
So each character have its own ASCII number.
for a it is 97 for b it is 98 similar for z it is 122
Also for A it is 65 for B it is 66 and for Z it is 90.
We can check each character in character array if its values is between 97 to 122 then we can substract 32 from that character to get its Capital letter.
Using loops we are going to explain c program to convert lowercase to uppercase without using library function
Steps for program is as below
- Declare a character array
char str[50];
- Accept character array from user
fgets(str,sizeof(str),stdin);
- Loop character array until you get ‘\0’ (represents last or character array)
for(i=0;str[i]!='\0';i++)
- Inside loop check if a character is between a to z
if(str[i]>='a' && str[i]<='z')
- If yes then subtract 32 from that character
str[i] -=32;
- You will get Uppercase string.
printf("Upper case string is :%s",str);
A. Using For Loop to convert lowercase character to uppercase character
For loop is used to get each character form start to end one by one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> int main() { char str[50]; int i=0; printf("enter String\n"); fgets(str,sizeof(str),stdin); printf("String is %s", str); for(i=0;str[i]!='\0';i++){ if(str[i]>='a' && str[i]<='z'){ str[i] -=32; } } printf("Upper case string is :%s",str); } |
Output
1 2 3 4 | enter String Hello How are you String is Hello How are you Upper case string is :HELLO HOW ARE YOU |
In above program we checked str[i]
with a and z.
Instead of characters it can also use a and z ASCII values as below
1 2 3 | if(str[i]>='97' && str[i]<='122'){ str[i] -=32; } |
B. Using While Loop to convert lowercase character to uppercase character
Below program uses while loop to iterate character array. Based on ascii value we check character is in upper case or in lower case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h> int main() { char str[50]; int i=0; printf("enter String\n"); fgets(str,sizeof(str),stdin); printf("String is %s", str); i=0; while(str[i]!='\0'){ if(str[i]>=97 && str[i]<=122){ str[i] -=32; } i++; } printf("Upper case string is :%s",str); } |
C. Using Do While Loop to convert lowercase character to uppercase character
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<stdio.h> int main() { char str[50]; int i=0; printf("enter String\n"); fgets(str,sizeof(str),stdin); printf("String is %s", str); i=0; do{ if(str[i]>=97 && str[i]<=122){ str[i] -=32; } i++; } while(str[i]!='\0'); printf("Upper case string is :%s",str); } |
2. Using toupper() function to convert lowercase character to uppercase character
C program to convert lowercase to uppercase using string function
Using toupper()
function can easily convert lowercase character to upper case.
To use toupper()
you have to include ctype.h
header file
Syntax of toUpper() is
int toupper(int c);
This function takes one int argument and convert in to Upper case character and return its integer value.
Steps to convert lowercase character to uppercase is as below
- Declare a character array
char str[50];
- Accept character array from user
fgets(str,sizeof(str),stdin);
- Loop character array until you get ‘\0’ (represents last or character array)
for(i=0;str[i]!='\0';i++)
- Inside loop use toupper() method to convert lower case character to upper case
str[i]=toupper(str[i]);
- You will get Uppercase string.
printf("Upper case string is :%s",str);
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<stdio.h> #include<ctype.h> int main() { char str[50]; int i=0; printf("enter String\n"); fgets(str,sizeof(str),stdin); printf("String is %s", str); for(i=0;str[i]!='\0';i++){ str[i]=toupper(str[i]); } printf("Upper case string is :%s",str); } |
3. Creating a function to convert lowercase character to uppercase character
A. Passing each character to convert it to uppercase
Here one one character of character string is converted to Upper case using Function toUpper()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> char toUpper(char ch){ if(ch>='a' && ch<='z'){ ch -=32; } return ch; } int main() { char str[50]; int i=0; printf("enter String\n"); fgets(str,sizeof(str),stdin); printf("String is %s", str); for(i=0;str[i]!='\0';i++){ str[i]=toUpper(str[i]); } printf("Upper case string is :%s",str); } |
B Passing entire String to Covert to upper case
Here entire character string is passed to a function and converted to upper case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> void toUpperCase(char str1[]){ int i=0; for(i=0;str1[i]!='\0';i++){ if(str1[i]>='a' && str1[i]<='z'){ str1[i] -=32; } } } int main() { char str[50]; printf("enter String\n"); fgets(str,sizeof(str),stdin); printf("String is %s", str); toUpperCase(str); printf("Upper case string is :%s",str); } |
We have discussed simple methods of conversion lower case to upper case in c. You can apply any one of the above as per your need.
Here we discussed c program to convert lowercase character to uppercase character. Hope you understood any more query email us.