C program to convert lowercase character to uppercase character

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.

  1. Using Loop- for, while and do while loops
  2. Using Predefined function toupper()
  3. Creating own function

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

  1. Declare a character array char str[50];
  2. Accept character array from user fgets(str,sizeof(str),stdin);
  3. Loop character array until you get ‘\0’ (represents last or character array) for(i=0;str[i]!='\0';i++)
  4. Inside loop check if a character is between a to z if(str[i]>='a' && str[i]<='z')
  5. If yes then subtract 32 from that character str[i] -=32;
  6. 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

Output

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

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.

C. Using Do While Loop to convert lowercase character to uppercase character

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

  1. Declare a character array char str[50];
  2. Accept character array from user fgets(str,sizeof(str),stdin);
  3. Loop character array until you get ‘\0’ (represents last or character array) for(i=0;str[i]!='\0';i++)
  4. Inside loop use toupper() method to convert lower case character to upper case str[i]=toupper(str[i]);
  5. You will get Uppercase string. 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().

B Passing entire String to Covert to upper case

Here entire character string is passed to a function and converted to upper case.

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.

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Command Line Argument
  4. Local vs Global Object in C++
  5. Local and Nested Classes in C++
  6. Arithmetic operations using switch case
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++