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);
    }
}

Ads









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 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
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... of the program: Enter a String Roseindia technology
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
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 '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 '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
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
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
. Description of the code: In the program code given below, we will find another String... Java String Occurrence in a String       In this program you will learn how to find the occurrence
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

Ads