Home Answers Viewqa Java-Beginners Program to swap the string

 
 


Akash Agrawal
Program to swap the string
1 Answer(s)      9 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!!

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 Pages:
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
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
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 two any numbers (from Keyboard)
Swap two any numbers (from Keyboard)       This is a simple Java Oriented  language program... will learn how to swap or exchange the number to each other. First of all we have
string program
string program  write a program to print the pattern p r o program r a m
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
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 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
string
string  a java program to replace all matching substrings
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  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 http
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 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 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.  
Swapping of two numbers
program to calculate swap of two numbers. Swapping is used where  you want to interchange the values. This program will help you to increase your programming ability. In this program we will see how we can swap two numbers. We can do
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
program
program  write a program reversee a string. input-hi this is roseindia output-roseindia is this hi
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
program
program  Write a program to print details of a week for a particular day using array of objects Integer and String. OUTPUT: First Day of Week... Seventh Day of Week is Sunday Note: Use two dimensional array of String
Program
?? using string Buffer class and perform the following operations. i) Append s1 and s2. ii) Reverse string s1. iii) Find length of string s1 and s2. iv) Replace string s1 to â??Simpleâ??. v) Extract 0th to 6th character from string s2 into s3
program
program  Given a string and a number ââ?¬Ë?nââ?¬â?¢, find the ââ?¬Ë?nââ?¬â?¢th distinct repeating character
program
an example of your problem. public class College { String str = "IIT...); } public static void main(String args[]) { College
Program
import="java.util.*"%> <% String id[]= new String[10]; for(int i=0;i<10;i++){ id[i]=request.getParameter("radio"+i); } String str[]= new String[10
program
{ // Getting query String qry; qry = req.getParameter
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
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
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
string manipulation
string manipulation  Write a program in java and accept 10 strings form the user. store them in an array. then find the string with the maaximum length and print
string prgm
string prgm  write a java program which read a sentence and print occurence of each vowels in it seperatly
string manipulation
string manipulation  write a java program that asks the user to enter a string(where a consonant means insert, a punctuation means return element... empty priority queue.The program will output the sequence of values returned
string manipulation
string manipulation  write a java program that asks the user to enter a string(where a consonant means insert, a punctuation means return element... empty priority queue.The program will output the sequence of values returned
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
string object
string object  Hi, how do i write a program that works with a user's password, when the program should prompt the user for a possible password, that is between 6 and 10 characters long, that contains at least one letter
searching string
searching string   how to write a program for searching a string...{ public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.println("Enter string: "); String str = input.nextLine
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
integer to string
integer to string  i have to develop a program which convert integer into character from 1 to 10,000 if we input 1 then it give output 'one' n so on till 'ten thousand' kindly give me the codes plzplz sir........   Have