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

What is the difference between method overloading and method overriding in c++?

0 Answers  


what is difference between internet and Internet?

12 Answers   College School Exams Tests, Microsoft, MIT, TCS,


I want to write a C++ language program that: 1. Reads in the size of a square from the screen; 2. Prints a hollow square of that size out of “-“, “|” and blanks on screen; 3. Prints the same hollow square onto a text file. The program should work for squares of all side sizes between 1 and 20.

0 Answers  


write program for palindrome

81 Answers   Amazon, Aricent, CSC, GE, HCL, Infosys, Syntel, Temenos, Wipro,


What is the use of cmath in c++?

0 Answers  






What and all can a compiler provides by default?

3 Answers   Accenture, HP,


What is scope in c++ with example?

0 Answers  


Problem 6: Area of a trapezoid can be calculated by the following formula: A=(b1 b2)×h2 where b1 and b2 are the parallel sides or the bases and h is length of height Write a C code of this program

0 Answers   Maxobiz,


Write the program form Armstrong no in c++?

11 Answers   HCL,


What is an incomplete type?

1 Answers  


What is called array?

0 Answers  


Are vectors passed by reference c++?

0 Answers  


Categories