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



Write a program to swap 2 chars without using a third varable? char *s = "A"; char *p ..

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

Write a program to swap 2 chars without using a third varable? char *s = "A"; char *p ..

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

Write a program to swap 2 chars without using a third varable? char *s = "A"; char *p ..

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

Write a program to swap 2 chars without using a third varable? char *s = "A"; char *p ..

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 = &ac;
char *b = &bc;
swap(a,b);
}

Is This Answer Correct ?    12 Yes 13 No

Write a program to swap 2 chars without using a third varable? char *s = "A"; char *p ..

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

Write a program to swap 2 chars without using a third varable? char *s = "A"; char *p ..

Answer / answer and question

k=*s;
*s=*p;
*p=k;

Is This Answer Correct ?    0 Yes 7 No

Write a program to swap 2 chars without using a third varable? char *s = "A"; char *p ..

Answer / s.divya

void swap(A,B)
{
A=*p;
B=*s;
getch();
}

Is This Answer Correct ?    1 Yes 18 No

Post New Answer

More C++ General Interview Questions

Does a derived class inherit or doesn't inherit?

0 Answers  


What is volatile and pragma? When they are used?

1 Answers  


What is the output of this prog. ? struct A { A(){ cout << \"A\"; } }; struct B { B(){ cout << \"B\"; } }; struct C { C(){ cout << \"C\"; } }; struct D { D(){ cout << \"D\"; } }; struct E : D { E(){ cout << \"E\"; } }; struct F : A, B { C c; D d; E e; F() : B(), A(),d(),c(),e() { cout << \"F\"; } };

2 Answers  


Define the process of handling in case of destructor failure?

0 Answers  


Can a Structure contain a Pointer to itself?

0 Answers  






Which software is best for programming?

0 Answers  


Write a program using shift_half( ) function to shift the elements of first half array to second half and vice versa.

0 Answers  


How many standards of c++ are there?

0 Answers  


Explain the difference between new() and malloc() in c++?

0 Answers  


What is low level language in simple words?

0 Answers  


Explain one method to process an entire string as one unit?

0 Answers  


How to allocate memory dynamically for a reference?

0 Answers  


Categories