Program to swap the string

Program to swap the string

Hi,

can any one tell me the code to swap the 2 strings without using temporary varibales???

Thanks a lot in advance!!

View Answers

August 25, 2012 at 5:12 PM

Here is an example that prompts the user to enter two string and then swaps both the string without using third variable.

import java.util.*;

class SwapTwoStringsWithoutUsingThirdVariable 
{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter string1: ");
        String st1=input.next();

        System.out.print("Enter string2: ");
        String st2=input.next();
        System.out.println();
        System.out.println("Before swapping:");
        System.out.println("st1="+st1+" and st2="+st2);
        st1=st1+st2;
        st2 = st1.substring(0,(st1.length()-st2.length()));
        st1 = st1.substring(st2.length(),(st1.length()));         

        System.out.println();
        System.out.println("After swapping:");
        System.out.println("st1="+st1+" and st2="+st2);
    }
}









Related Tutorials/Questions & Answers:
Program to swap the string
Program to swap the string  Hi, can any one tell me the code to swap...!!   Here is an example that prompts the user to enter two string and then swaps both the string without using third variable. import java.util.*; class
string program
string program  write a program to print the pattern p r o program r a m
Advertisements
string program
string program  wap to find single occurrence of given string in java without using any predefined method
string program
string program  reads sentence and find the average length of word... AverageLengthOfWords { public static void main(String[] args...); System.out.print("Enter sentence: "); String st=input.nextLine(); String str
String program - Java Beginners
String program  write a program to accept a word. Pass it to the function magic(String x). The function checks the string for the presence... prints it is a magic string otherwise it prints not a magic string.  
String reverse program
String reverse program  write a java program for reverse string? if the string is "Hi welcome to hyderabad" the reverse string is "hyderabad... for help. You can split the String into String array using split method of String
java string vowel program
java string vowel program  java string vowel program   Please visit the following links: Display the word that begins with vowel Java Count Vowels
String Function Program
String Function Program  The string function program is just an application of the string manipulation functions in JAVA. The program is designed to count the number of one-letter word, two-letter word, and three-letter words
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
java program for string tokenzing and parsing
java program for string tokenzing and parsing  String s="height=128 weight=22 gender=male"; i need to convert this into height="128" weight="22" gender="male" normally if we print "hello" means we use System.out.println("\"Hello
java program for string tokenzing and parsing
java program for string tokenzing and parsing  String s="height=128 weight=22 gender=male"; i need to convert this into height="128" weight="22" gender="male" normally if we print "hello" means we use System.out.println("\"Hello
A Program To Reverse Words In String in Java
A Program To Reverse Words In String in Java  A Program To Reverse Words In String in Java for example: Input:- Computer Software Output... Tell Me This Program.  Here is an example that reverse the words
String Exception Program - JSP-Servlet
String Exception Program  Respected Sir/Madam, I am... the code for your kind reference: <% int val =0; String contentType..., totalBytesRead, formDataLength); totalBytesRead += byteRead; } String file = new
Reverse String Program in Java
Reverse String Program in Java We are going to describe about reverse string program in java. In this example reverses a string entered by the user. We... Output- Here is the output of the program: Enter a String
ModuleNotFoundError: No module named 'parent-swap'
ModuleNotFoundError: No module named 'parent-swap'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'parent-swap' How to remove the ModuleNotFoundError: No module named '
ModuleNotFoundError: No module named 'add-swap'
ModuleNotFoundError: No module named 'add-swap'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'add-swap' How to remove the ModuleNotFoundError: No module named 'add-swap
ModuleNotFoundError: No module named 'extension-swap'
ModuleNotFoundError: No module named 'extension-swap'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'extension-swap' How to remove the ModuleNotFoundError: No module named
ModuleNotFoundError: No module named 'extension-swap'
ModuleNotFoundError: No module named 'extension-swap'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'extension-swap' How to remove the ModuleNotFoundError: No module named
Program using String functions - Java Beginners
Program using String functions  Write a program to accept a sentence... { public static void main(String[] args) { int count = 0...); System.out.print("Enter string:"); String st = input.nextLine
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
ModuleNotFoundError: No module named 'push-swap-gui'
ModuleNotFoundError: No module named 'push-swap-gui'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'push-swap-gui' How to remove the ModuleNotFoundError: No module named
Out of bounce exception in the string reverse program - Java Beginners
Out of bounce exception in the string reverse program  In the given program below this is taken from ur site but when i am executing it.It is giving an exception like string out of bounce.So please give me the correct solution
I am writing a program which converts a string to a timestamp and add days to it
I am writing a program which converts a string to a timestamp and add days to it  java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2012-01-11 12:34:39"); long l=ts2.getTime() +(100060602424);//add 24 days Date d=new Date(l
string
string  a java program using string function to input any string... ArrangeStringAlphabetically { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter string
Java String Occurrence in a String
Java String Occurrence in a String       In this program you will learn how to find the occurrence of a String in another String. Here we have already taken a String
string
string  write a program to accept the string and store the reverse stream into another array and print
String
String  write a program using string it should replace 'c'char to integer number as 1 in whole source
string
string  a java program to input a string and display the string...*; import java.io.*; public class FirstLetter{ public static String capitalizeFirstLetter( String str ) { final StringTokenizer st = new StringTokenizer( str
string
string   just i want to a program in a short form to the given string in buffered reader for example input string: Suresh Chandra Gupta output: S. C...; public class StringTest { public static void main(String [] args
string
string  program for String reverse and replace in c,c++,java e.g.Input:10,20,hello output:hello;20;10   Hi Friend, Try the following java...(String[] args) { String array[]=new String[5]; Scanner input
string
string  a java program using string function that computes your initials from your full name and display them   Please visit the following links: http://www.roseindia.net/tutorial/java/core/printInitials.html
STRING.....
STRING.....  plzz sent me d code for counting vowels in a string... gui programme
string
string   difference detween "public static void main (String[] args) " and "public static void main (String args[])" in java but it executes both... "String args[]" can mean a "string array called args which is an array
write a java program for inserting a substring in to the given main string from a given position
write a java program for inserting a substring in to the given main string from a given position  write a java program for inserting a substring in to the given main string from a given position
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.  write a program to create an arraylist with string(add,remove) operation.and value should be enter through
write a program to create an vector with string(add,remove) operation.and value should be enter through keyboard.
write a program to create an vector with string(add,remove) operation.and value should be enter through keyboard.  write a program to create an vector with string(add,remove) operation.and value should be enter through keyboard
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.  write a program to create an arraylist with string(add,remove) operation.and value should be enter through
string
string  a java program using string function and access modifiers to automate the insurance company which will have to manage the information about policy holders Data Members: 1)Policy No 2)Name of policy holder 3)Address 4
string
string  String helloString = new String(helloArray); System.out.println(helloString); i am unable to understand this. could u plz explain
String
String  how to add spaces with string functions.?   Hi... { public static String addSpaceToRight(String s, int n) { return String.format("%1$-" + n + "s", s); } public static String addSpaceToLeft(String s, int n
String
characters in string?   import java.util.*; class RemoveDuplicateCharatcersFromString { public static String removeDuplicates(String s... < s.length(); i++) { String st = s.substring(i, i + 1
Image swap on mouse over and mouse move
Image swap on mouse over and mouse move  i tried the code , sent form u...But if there are 50 images and..i want swap images on mousse over and mouse out, then the code becomes very lengthy...so please give the code for multiple
Image swap on mouse over and mouse move
Image swap on mouse over and mouse move  i tried the code , sent form u...But if there are 50 images and..i want swap images on mousse over and mouse out, then the code becomes very lengthy...so please give the code for multiple
String
String  write down the code of remove any character from a given string without using any string function   please give me the code of remove any given character from a given string without using function

Ads