TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Swap two String Variables Without Using Third or Temp Variable

Program to Swap two String Variables Without Using Third or Temp Variable on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

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

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.

Str1 = Str1 + Str2= Goodmorning

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.

Solution

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

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf