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 |
Define a conversion constructor?
What is malloc in c++?
How do you initialize a class member, class x { const int i; };
Explain queue. How it can be implemented?
Write a struct time where integer m, h, s are its members?
If you hear the cpu fan is running and the monitor power is still on, but you did not see anything show up in the monitor screen. What would you do to find out what is going wrong?
Should a constructor be public or private?
How many storage classes are available in C++?
What is the benefit of learning c++?
How do you invoke a base member function from a derived class in which you’ve overridden that function?
We all know that a const variable needs to be initialized at the time of declaration. Then how come the program given below runs properly even when we have not initialized p?
Explain the extern storage classes in c++.