Your Ad Here

Thursday, February 26, 2009

Write a C++ program that uses functions to swap two characters.

#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void swap(int, int);
void main( )
{
char s1, s2;
clrscr( );
cout<<"Enter the two characters to swap:";
cin>>s1>>s2;
swap(s1, s2);
getch( );
}
void swap(char c1, char c2)
{
char c3;
cout<<"Characters before swapping:"<< c1<<"and"<< c2;
c3=c1;
c1=c2;
c2=c3;
cout<<"Characters after swapping:"<< c1<<"and"<< c2;
}

Output: Enter the two characters to swap: a b

Values before swapping: a and b
Values after swapping: b and a

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

No comments:

Post a Comment