How to swap two String values without using a
third variable?

Answers were Sorted based on User's Feedback



How to swap two String values without using a third variable?..

Answer / shyambaishya

Using Java:
===========
String s1 = "Shyam Sundar Baishya" ;
String s2 ="Dilip Kumar";
System.out.println("Before swapping ............");
System.out.println("s3 == "+s1);
System.out.println("s4 == "+s2);
s1= s1+s2;
System.out.println("....................");
System.out.println("After swapping ............");
s2 = s1.substring(0,(s1.length()-s2.length()));
s1 = s1.substring(s2.length(),(s1.length()));
System.out.println("s3 == "+ s1);
System.out.println("s4 == "+ s2);

Is This Answer Correct ?    49 Yes 10 No

How to swap two String values without using a third variable?..

Answer / parthag

In C#, C sharp

string str1 = "First";
string str2 = "Second";
str1 = string.Concat(str1, str2);
str2 = str1.Replace(str2,"");
str1 = str1.Replace(str2,"");

Is This Answer Correct ?    5 Yes 0 No

How to swap two String values without using a third variable?..

Answer / badar awan

public class B
{
public static void main(String [] args)
{
String a = "Badar" ;
String b ="Asad";
a= a+b;
b = a.substring(0,(a.length()-b.length()));
a = a.substring(b.length(),(a.length()));
System.out.println("a == "+ a);
System.out.println("b == "+ b);
}
}

Is This Answer Correct ?    9 Yes 5 No

How to swap two String values without using a third variable?..

Answer / parthag

In C#

string str1 = "First";
string str2 = "Second";
str1 = string.Concat(str1, str2);
str2 = str1.Replace(str2,"");
str1 = str1.Replace(str2,"");

Is This Answer Correct ?    2 Yes 0 No

How to swap two String values without using a third variable?..

Answer / andy

'Swapping two strings in vb.net
Private Function SwappStrings(ByVal str1 As String,
ByVal str2 As String) As String
str1 = str1 + str2
str2 = str1.Substring(0, (str1.Length -
str2.Length))
str1 = str1.Substring(str2.Length)
SwappStrings = "A is : " & str1.ToString & "And B
is : " & str2.ToString
End Function

Is This Answer Correct ?    6 Yes 6 No

How to swap two String values without using a third variable?..

Answer / bharat

import java.util.Scanner;

public class Swap {

/**
* @param args
*/


public static void main(String[] args) {
// TODO Auto-generated method stub
int a, b;
System.out.println("Enter a and b");
Scanner in = new Scanner(System.in);

a = in.nextInt();
b = in.nextInt();
System.out.println("Befor Swapinng :" + "a : " + a + "\t" + "b :" + b);
b = a + b;
a = b - a;
b = b - a;
System.out.println("a : " + a + "\t" + "b :" + b);

}

}

Is This Answer Correct ?    0 Yes 0 No

How to swap two String values without using a third variable?..

Answer / gayitri91

import java.io.*;

class Swap_2_Strings
{
String s1,s2;
DataInputStream dis=new DataInputStream(System.in);


Swap_2_Strings()
{
try
{
int n,m;
System.out.println("Enter 2 strings");
s1=dis.readLine();
s2=dis.readLine();
n=s1.length();
m=s2.length();
s1=s1+s2;
s2=s1.substring(0,n);

s1=s1.substring(n,n+m);

System.out.println("s1="+s1+"\n s2="+s2);

}
catch(IOException x)
{
System.out.println(x);
}
}
public static void main(String arg[])
{
Swap_2_Strings s=new Swap_2_Strings();
}
}

Is This Answer Correct ?    7 Yes 8 No

How to swap two String values without using a third variable?..

Answer / jose

Q:john william
A:William john

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More Programming Languages AllOther Interview Questions

Difference between delegates and Events?

0 Answers  


Create a class TicTacToe that will enable you to write a complete program to play the game of Tic-Tac-Toe. The class contains as private data a 3- by-3 double array of integers. The constructor should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place a 1 in the specified square; place a 2 wherever the second player moves. Each move must be to an empty square. After each move determine whether the game has been won and whether the game is a draw. If you feel ambitious, modify your program so that the computer makes the moves for one of the players automatically. Also, allow the player to specify whether he or she wants to go first or second. If you feel exceptionally ambitious, develop a program that will play three-dimensional Tic-Tac-Toe on a 4-by-4-by-4

2 Answers  


What is autocall macro and how to create autocall macro? what is the use of it?

0 Answers   GE,


is try block possible without catch block?

3 Answers  


the systematic access of small computers in a distributed data processing system is referred as?

0 Answers   Mphasis,






What is the use of sas software? Is sas and sap are different?

0 Answers  


What is the language used for Artificial Intelligence?

1 Answers  


sample code for data transfer between two r/2 systems and r/3 systems?

0 Answers  


Is class is a abstract datatype in java?

0 Answers  


how can we maintain the previous version scripts to new version.

0 Answers   DST Global Solutions,


If i have a dataset queried from Sql and I would like to insert the dataset into a specific node in an xml document how do I do this

0 Answers   SGT,


how to swap all the values without using temporary variable. tha values r a = 20, x=60 and p=2.

5 Answers   DST Global Solutions, iGate,


Categories