Reversing Case of Character

CCBeginner
Practice Now

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).


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123328{{"`Reversing Case of Character`"}} c/comments -.-> lab-123328{{"`Reversing Case of Character`"}} c/variables -.-> lab-123328{{"`Reversing Case of Character`"}} c/data_types -.-> lab-123328{{"`Reversing Case of Character`"}} c/operators -.-> lab-123328{{"`Reversing Case of Character`"}} c/if_else -.-> lab-123328{{"`Reversing Case of Character`"}} c/function_declaration -.-> lab-123328{{"`Reversing Case of Character`"}} end

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:

  1. Creating a new C file
  2. Including header files
  3. Writing the main() function
  4. Getting user input
  5. Reversing the case of the character
  6. Displaying the output

You can now use this program to reverse the case of any input character in C.

Other C Tutorials you may like