Write a programmer defined function with void return type that swaps two charact
ID: 3801911 • Letter: W
Question
Write a programmer defined function with void return type that swaps two characters. Your main function must declare two integers and ask the user for their values. In your main function, display the characters as well as the memory addresses of the respective variables. Now, call your function to swap the value in the character variables. Inside your function, display the value of the characters before and after swapping, and the memory addresses occupied by the variables in your function. After returning to your main function, display the value of character variables. Use pass by reference method when calling your function.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void swap(char *a,char *b)
{
char *temp;
*temp = *a; //swapping values using temp variable
*a = *b;
*b = *temp;
}
int main(void)
{
int x,y;
char a,b;
printf("Enter the values of x and y : ");
scanf("%d %d",&x,&y);
//converting integers to characters
a = x + '0';
b = y + '0';
printf(" Corresponding characters a = %c b = %c ",a,b);
printf(" Address of a = %u Address of b = %u",&a,&b);
swap(&a,&b);
printf(" After Swapping , a = %c b = %c",a,b);
return 0;
}
output:
Enter the values of x and y : 65 66
Corresponding characters a = q b = r
Address of a = 3217213030 Address of b = 3217213031
After Swapping , a = r b = q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.