Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Program to swap the string
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Akash Agrawal
Program to swap the string
1 Answer(s)      2 years and 4 months ago
Posted in : Java Beginners


Hi,

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

Thanks a lot in advance!!


Advertisement
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.  
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 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
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... of the program: Enter a String Roseindia technology
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
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 numbers without using third variable
Swap two numbers without using third variable In this section we are going to swap two variables without using the third variable. For this, we have used...: import java.util.*; class Swapping { public static void main(String[] args
Swap images oon mouse over and mouse out
Swap images oon mouse over and mouse out  I have multiple images...i want to swap each image on mouse out and mouse over..   <html> <script language="javascript" type="text/javascript"> if(document.images
Image swap on MouseOver ann Mouse out
Image swap on MouseOver ann Mouse out  I tried ur code for multiple images but didnt get the right answer...i dont want the hardcodded code...a code which is flexible with multiple images <html> <script language
Swap two xml nodes through java class - XML
Swap two xml nodes through java class   Could you please let me know How to swap the two xml nodes thorugh java class
Swap two any numbers
Swap Any Two Numbers       This is a simple Java Oriented  language program. If  you... to swap or exchange the number to each other. First of all we have to create
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
Java Panel squres swap - Java Server Faces Questions
Java Panel squres swap  I have written a coputer game that has 8*8 squres and it has 7 different images stored in the array now placing them on the form i have done that the problem is to swap them veticaly and horizontaly
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  write a program using string it should replace 'c'char to integer number as 1 in whole source
string
string  write a program to accept the string and store the reverse stream into another array and print
Advertisements
 

 

 

DMCA.com