Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise: Its a Case of Swapping (pair) This is a pair exercise to complete with

ID: 3709246 • Letter: E

Question

Exercise: Its a Case of Swapping (pair) This is a pair exercise to complete with your lab partner. Write a C program swap_case.c which reads characters from its input and writes the same characters to its output with lower case letters converted to upper case and upper case letters converted to lower case Your program should stop only at the end of input. For example: ? dcc swap_case.c -o Swap_case ? ./swap_case Are you saying 'Boo' or 'Boo-Urns'? aRE YOU SAYING 'bOO' OR 'boo-uRNS'? In this house, we obey the laws of thermodynamics! ?N THIS HOUSE, WE OBEY THE LAWS OF THERMODYNAMICS! UPPER !@#5% lower upper !@#SS LOWER Ctrl-d Hint: hint use getcharto read characters (don't use scanf or fgets) Hint: you need only a single int variable. Don't use an array. Hint: use putchar to output each character Hint: make sure you understand this example program which reads characters until end of input. Hint: make sure you understand this example program which reads characters, printing them with lower case letters converted to upper case. Hint: create a function with a prototype like this: int swap_case(int character); which: . returns the character in lower case if it is an upper case letter returns the character in upper case if it is a lower case letter . returns the character unchanged otherwise

Explanation / Answer

Solution:

Please note the following program takes one character, sends it to swap_case() and gets back the upoer-case / lower-case character. putchar() and getchar() are being used as instructed.
Please note, here it is mentioned that we should not used array. In that case we will not be able to store the whole input string, as getchar() will take only the first character from the input. But as mentioned we in problem statement, it is not clear how the whole input string will be parsed and converted once the progran runs.
In order to provide solution for swap_case() we have completed the implementation along with a full progran. This method as required will take only one int as an arguement and return back the converted value.

#include<stdio.h>

int swap_case(int character);

int main() {
int character;

printf("Enter the character:") ;
character = getchar() ;
putchar(swap_case(character)) ;

return (0);
}

int swap_case(int character) {
//check if character is upper case
if (character >= 97 && character <= 122){
character = character - 32;
}
//check if character is lower case
else if (character >= 65 && character <= 90){
character = character + 32;
}
//otherwise send back the character as it is
return character;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote