Introduction
This lab will guide you through the process of writing a C program to reverse the case of input character. This program will take a user input character and convert it to its opposite case (lowercase to uppercase or uppercase to lowercase).
Creating a new C file
To begin, create a new C file named main.c in the ~/project/ directory.
Including header files
The first step is to include the necessary header files in the program. In this program, we will need to include the following header files:
#include<stdio.h>
#include<ctype.h>
The stdio.h header file provides input and output functions, while the ctype.h header file provides functions to check whether a character is uppercase or lowercase.
Writing the main() function
The next step is to declare the main() function and initialize the variables. In this program, we will use the char data type to store the character input.
int main()
{
char alphabet;
// Your code goes here
return 0;
}
Getting user input
Prompt the user to input a character using printf(). Use the getchar() function to read the user's input.
printf("Enter a character: ");
alphabet = getchar();
Reversing the case of the character
Use the islower() function from the ctype.h header file to check whether the character is lowercase or not. If it is lower case, use the toupper() function to convert it to uppercase, and vice versa using the tolower() function.
if(islower(alphabet))
alphabet = toupper(alphabet);
else
alphabet = tolower(alphabet);
Displaying the output
Print the reversed case character using the printf() function.
printf("The character in opposite case is: %c\n", alphabet);
Putting it together
Here is the complete code for the program:
#include<stdio.h>
#include<ctype.h>
int main()
{
char alphabet;
printf("Enter a character: ");
alphabet = getchar();
if(islower(alphabet))
alphabet = toupper(alphabet);
else
alphabet = tolower(alphabet);
printf("The character in opposite case is: %c\n", alphabet);
return 0;
}
Summary
In this lab, you learned how to write a C program to reverse the case of an input character. We covered the following steps:
- Creating a new C file
- Including header files
- Writing the main() function
- Getting user input
- Reversing the case of the character
- Displaying the output
You can now use this program to reverse the case of any input character in C.



