Q:

Program to swap two string variables without using third or temp variable

belongs to collection: String Programs

0

Explanation

In this program, we need to swap two strings without using a third variable.

Str1: Good Str2: morning  

Swapping two strings usually take a temporary third variable. One of the approach to accomplish this is to concatenate given two strings into first string.

Str1Str1 = Str1 + Str2Goodmorning  

Extract string 2 using substring (0, length(string1) - (string2)) i.e. in our case it will be substring(0, (11-4)). It will assign string Good to string 2 which is highlighted by green.

Str2 = Goodmorning  

Extract string 1 using substring (length(string2)) i.e. we need to extract string from in length(string2) till end of the string. In our case it will be substring(4). It will assign string morning to string 1 which is highlighted by green.

Str1 = Goodmorning  

Algorithm

  1. Define two strings that need to be swapped.
  2. Concatenate both the strings and store it in first string.
  3. Extract the string from indexes 0 to length (string1) - (string2) using substring function and store it in string 2.
  4. Extract the string from index length (string2) till end using substring function and store it in string 1.

Input:

str1 = "Good";  

str2 = "morning";  

Output:

Strings before swapping: Good morning
Strings after swapping: morning Good

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

str1 = "Good";  
str2 = "morning";  
   
print("Strings before swapping: " + str1 + " " + str2);  
   
#Concatenate both the string str1 and str2 and store it in str1  
str1 = str1 + str2;  
#Extract str2 from updated str1  
str2 = str1[0 : (len(str1) - len(str2))];  
#Extract str1 from updated str1  
str1 = str1[len(str2):];  
   
print("Strings after swapping: " + str1 + " " + str2);  

 

Output:

Strings before swapping: Good morning
Strings after swapping: morning Good

 

C

#include <stdio.h>  
#include <string.h>  
   
int main()  
{  
    char str1[20] = "Good", str2[20] = "morning";  
      
    printf("Strings before swapping: %s %s\n", str1, str2);  
      
    //User-defined substring function that will take string(str), position(p) and no of character(len) as input  
    //Produces result sub as output  
    void substring(char s[], char sub[], int p, int len){  
       int c = 0;  
       while (c < len) {  
          sub[c] = s[p+c];  
          c++;  
       }  
       sub[c] = '\0';  
    }  
      
    //Concatenate both the string str1 and str2 and store it in str1  
    strcat(str1, str2);  
    //Extract str2 from updated str1  
    substring(str1, str2, 0, (strlen(str1) - strlen(str2)));  
    //Extract str1 from updated str1  
    substring(str1, str1, strlen(str2), strlen(str1));  
     
    printf("Strings after swapping: %s %s", str1, str2);  
      
    return 0;  
}  

 

Output:

Strings before swapping: Good morning
Strings after swapping: morning Good

 

JAVA

public class SwapStrings   
{  
    public static void main(String[] args) {  
        String str1 = "Good", str2 = "morning";  
          
        System.out.println("Strings before swapping: " + str1 + " " + str2);  
          
        //Concatenate both the string str1 and str2 and store it in str1  
        str1 = str1 + str2;  
        //Extract str2 from updated str1  
        str2 = str1.substring(0, (str1.length() - str2.length()));  
        //Extract str1 from updated str1  
        str1 = str1.substring(str2.length());  
          
        System.out.println("Strings after swapping: " + str1 + " " + str2);  
    }  
}  

 

Output:

Strings before swapping: Good morning
Strings after swapping: morning Good

 

C#

using System;  
                      
public class SwapStrings  
{      
    public static void Main()  
    {  
        String str1 = "Good", str2 = "morning";  
          
        Console.WriteLine("Strings before swapping: " + str1 + " " + str2);  
          
        //Concatenate both the string str1 and str2 and store it in str1  
        str1 = str1 + str2;  
        //Extract str2 from updated str1  
        str2 = str1.Substring(0, (str1.Length - str2.Length));  
        //Extract str1 from updated str1  
        str1 = str1.Substring(str2.Length);  
          
        Console.WriteLine("Strings after swapping: " + str1 + " " + str2);  
    }  
}  

 

Output:

Strings before swapping: Good morning
Strings after swapping: morning Good

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
$str1 = "Good";  
$str2 = "morning";  
   
print("Strings before swapping: " . $str1 . " " . $str2);  
   
//Concatenate both the string str1 and str2 and store it in str1  
$str1 = $str1 . $str2;  
//Extract str2 from updated str1  
$str2 = substr($str1, 0, (strlen($str1) - strlen($str2)));  
//Extract str1 from updated str1  
$str1 = substr($str1, strlen($str2));  
   
print("<br>Strings after swapping: " . $str1 . " " . $str2);  
?>  
</body>  
</html>  

 

Output:

Strings before swapping: Good morning
Strings after swapping: morning Good

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


Program to count the total number of words in a st... >>
<< Program to separate the individual characters from...