Write a program to swap 2 chars without using a third
varable?
char *s = "A";
char *p = "B";
Answers were Sorted based on User's Feedback
Answer / dooglus
#include <cstdio>
void swap(char *c, char *d)
{
*d = *c^*d; // c = C d = C^D
*c = *c^*d; // c = C^C^D d = C^D
*d = *c^*d; // c = C^C^D d = C^C^D^C^D
}
main()
{
char c = 'c';
char d = 'd';
swap(&c, &d);
}
Is This Answer Correct ? | 20 Yes | 3 No |
Answer / prasenjit roy
#include <stdio.h>
//No restrinction of datatype
#define SWAP(x,y) { x = x ^ y; \
y = x ^ y; \
x = x ^ y; \
}
void main()
{
char c = 'c';
char d = 'd';
SWAP(c, d);
}
Is This Answer Correct ? | 13 Yes | 2 No |
Answer / rajesh rvp
#include <stdio.h>
int main ()
{
int i;
char c,d,temp;
scanf("%c %c",&c,&d);
If (toascii (c)>toascii (d))
{
temp=c;
c=d;
d=temp;
}
return 0;
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / lior
void swap(char *s, char *p)
{
if(0 == s || 0 == p)
return;
*s += *p;
*p = *s - *p;
*s = *s - *p;
}
int main()
{
/* Use chars and not strings!! */
char ac = 'A';
char bc = 'B';
char *a = ∾
char *b = &bc;
swap(a,b);
}
Is This Answer Correct ? | 12 Yes | 13 No |
Answer / koushik sarkar
#include<stdio.h>
void swap(char *p,char *s){*p=*p+*s-(*s=*p);}
int main()
{
char a,b;
a='A';b='B';
printf("a=%c,b=%c",a,b);
swap(&a,&b);
printf("a=%c,b=%c",a,b);
return 0;
}
Is This Answer Correct ? | 4 Yes | 11 No |
Is c++ pass by reference or value?
What is the use of ‘using’ declaration?
What are the various operations performed on stack?
How does c++ sort work?
Write a function that swaps the values of two integers, using int* as the argument type?
What is #include iostream in c++?
How Virtual functions call up is maintained?
What is the best c c++ compiler for windows?
how many rounds and wt type of questios ask in the written test for first round 2. tech. round 3. and futher rounds
What you know about structures in C++?
0 Answers Agilent, ZS Associates,
Write a program in C++ for Fibonacci series
0 Answers Axtria, ITC Indian Tobacco Company,
How can I disable the "echo" feature?