C Program to convert lowercase to uppercase and vice-versa

February 7, 2022, Learn eTutorial

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

In this program, we have to convert the uppercase letters to lowercase and lowercase letters to uppercase. The logic of this program is, first include the header file string.h to handle the string operations. Read a sentence from the user and save it into the variable sentence. By using a for loop just traverse through it to get the count of the sentence. Then keep the length of the sentence into the variable count. Then display the string. Then by using a for loop with the condition i

What is islower() function in C?

The islower() function is used to check whether a character is lowercase or not. If the character is lowercase, then it will return a number that is greater than zero. Else it will return zero. The prototype of the function islower() is given below.

int islower(int ch);

The return type is an integer type. The argument is also an integer, which means the ASCII value of the character which we pass.



STEP 1:Include the header files to use the built-in header files into the C program.

STEP 2: Declare the variable count, ch,i as integer, and sentence as a character.

STEP 3:Read a sentence from the user into the variable sentence.

STEP 4:By using a for loop with the condition sentence[i]=getchar()!=null ,incremnet i by 1.

STEP 5:Set count =i.

STEP 6: Display the input sentence as a sentence.

STEP 7:Display the resultant sentence is using printf function.

STEP 8:By using a for loop with the condition i

STEP 9:check sentence[i] is in lowercase or not.If lowercase then changes to uppercase.

STEP 10:display sentence[i] using putchar() function.


C Source Code

                                          #include <stdio.h>
#include <ctype.h>

void main()
  char sentence[100];
  int count, ch, i;
  printf("Enter a sentence\n");
  for (i = 0;
    (sentence[i] = getchar()) != '\n'; i++)
  count = i;
  /*convert the lower case letters to upper case and vice-versa */
  printf("\nThe input sentence is %s ", sentence);
  printf("\nA resultant sentence is : ");
  for (i = 0; i < count; i++)
    ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
} /*End of main() */


Enter a sentence

good morning

The input sentence is:        good morning

A resultant sentence is:         GOOD MORNING