Merge two strings in chunks of given size

Given two strings ‘a’ and ‘b’ and a number k, our aim is to merge the strings into a string s such that it contains groups of k characters from the strings alternately in the final string.

Examples:

Input: a = "durability", 
      b = "essence
      k = 3
Output: duressabienclitey

Input: a = "determination"
      b = "stance"
      k = 3
Output: detstaermnceination

In the second example we chose groups of three characters from determination and stance alternately to make the string. But when the string stance was completely used then we added the remaining string of determination as it is.



The approach of this problem is to alternately add groups of characters of both the strings until one of the string is finished. Then we simply have to add the remaining portion of the leftover string to the answer as it is.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to merge n number of strings
#include <bits/stdc++.h>
using namespace std;
  
// Function performing the calculations
void solve(string a, string b, int k)
{
    string s = "";
  
    // Length of string a
    int la = a.length();
  
    // Length f string b
    int lb = b.length();
    int l = la + lb;
  
    // Pointers for string
    // a and string b
    int indexa = 0, indexb = 0;
    while (l > 0) {
  
        // pa and pb denote the number
        // of characters of both
        // a and b extracted
        int pa = 0, pb = 0;
  
        // If entire substring of length
        // k can be extracted
        if (la - indexa >= k) {
  
            // Please refer below link for
            // details of library function
            s = s + a.substr(indexa, k);
            indexa = indexa + k;
            pa = k;
        }
  
        // If the remaining string is
        // of length less than k
        else if (la - indexa < k && la - indexa > 0) {
            s = s + a.substr(indexa, la - indexa);
            pa = la - indexa;
            indexa = la;
        }
  
        // If the string has been
        // traversed
        else if (indexa >= la)
            pa = 0;
  
        // If entire substring of
        // length k can be extracted
        if (lb - indexb >= k) {
            s = s + b.substr(indexb, k);
            pb = k;
            indexb = indexb + k;
        }
  
        // If the remaining string is of
        // length less than k
        else if (lb - indexb < k && lb - indexb > 0) {
            s = s + b.substr(indexb, lb - indexb);
            pb = lb - indexb;
            indexb = lb;
        }
  
        // If the string has been
        // traversed
        else if (indexb >= lb)
            pb = 0;
        l = l - pa - pb;
    }
    cout << s;
}
  
// Driver function
int main()
{
    string a = "determination", b = "stance";
    int k = 3;
    solve(a, b, k);
    return 0;
}

chevron_right


JAVA

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to merge
// n number of strings
import java.io.*;
class msc {
    // Function performing calcuations
    public static void solve(String a,
                             String b, int k)
    {
        String s = "";
  
        // Length of string a
        int la = a.length();
  
        // Length f string b
        int lb = b.length();
        int l = la + lb;
  
        // Pointers for string a and string b
        int indexa = 0, indexb = 0;
        while (l > 0) {
  
            // pa and pb denote the number
            // of characters of both
            // a and b extracted
            int pa = 0, pb = 0;
  
            // If entire substring of
            // length k can be extracted
            if (la - indexa >= k) {
                s = s + a.substring(indexa, indexa + k);
                indexa = indexa + k;
                pa = k;
            }
  
            // If the remaining string is
            // of length less than k
            else if (la - indexa < k && la - indexa > 0) {
                s = s + a.substring(indexa, la);
                pa = la - indexa;
                indexa = la;
            }
  
            // If the string has been
            // traversed
            else if (indexa >= la)
                pa = 0;
  
            // If entire substring of
            // length k can be extracted
            if (lb - indexb >= k) {
                s = s + b.substring(indexb, indexb + k);
                pb = k;
                indexb = indexb + k;
            }
  
            // If the remaining string
            // is of length less than k
            else if (lb - indexb < k && lb - indexb > 0) {
                s = s + b.substring(indexb, lb);
                pb = lb - indexb;
                indexb = lb;
            }
  
            // If the string has been
            // traversed
            else if (indexb >= lb)
                pb = 0;
            l = l - pa - pb;
        }
        System.out.println(s);
    }
  
    // Driver function
    public static void main(String args[])
        throws IOException
    {
        String a = "determination", b = "stance";
        int k = 3;
        solve(a, b, k);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to merge n number of strings
  
# Function performing the calculations
def solve(a, b, k):
    s = ""
  
    # Length of string a
    la = len(a)
  
    # Length f string b
    lb = len(b)
    l = la + lb
  
    # Pointers for string
    # a and string b
    indexa = indexb = 0
      
    while l:
        # pa and pb denote the number
        # of characters of both
        # a and b extracted
        pa = pb = 0
  
        # If entire substring of length
        # k can be extracted
        if la - indexa >= k:
            s = s + a[indexa : indexa + k]
            indexa = indexa + k
            pa = k
  
        # If the remaining string is
        # of length less than k
        elif la - indexa < k and la - indexa:
            s = s + a[indexa : la]
            pa = la - indexa
            indexa = la
  
        # If the string has been
        # traversed
        elif indexa >= la:
            pa = 0
  
        # If entire substring of
        # length k can be extracted
        if lb - indexb >= k:
            s = s + b[indexb : indexb+k]
            pb = k
            indexb = indexb + k
  
        # If the remaining string is of
        # length less than k
        elif lb - indexb < k and lb - indexb:
            s = s + b[indexb : lb]
            pb = lb - indexb
            indexb = lb
  
        # If the string has been
        # traversed
        elif indexb >= lb:
            pb = 0
        l = l - pa - pb
          
    print(s)
  
  
# Driver function
a = "determination"; b = "stance"
k = 3
solve(a, b, k)
  
  
# This code is contributed by Ansu Kumari

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to merge 
// n number of strings
using System;
  
class GFG
{
    // Function performing
    // the calculations
    static void solve(string a, 
                      string b, 
                      int k)
    {
        string s = "";
      
        // Length of string a
        int la = a.Length;
      
        // Length f string b
        int lb = b.Length;
        int l = la + lb;
      
        // Pointers for string
        // a and string b
        int indexa = 0, indexb = 0;
        while (l > 0) 
        {
      
            // pa and pb denote the 
            // number of characters
            // a and b extracted
            int pa = 0, pb = 0;
      
            // If entire substring 
            // of length k can be 
            // extracted
            if (la - indexa >= k) 
            {
                s = s + a.Substring(indexa, k);
                indexa = indexa + k;
                pa = k;
            }
      
            // If the remaining string 
            // is of length less than k
            else if (la - indexa < k && 
                     la - indexa > 0) 
            {
                s = s + a.Substring(indexa, 
                                    la - indexa);
                pa = la - indexa;
                indexa = la;
            }
      
            // If the string has 
            // been traversed
            else if (indexa >= la)
                pa = 0;
      
            // If entire substring 
            // of length k can be
            // extracted
            if (lb - indexb >= k) 
            {
                s = s + b.Substring(indexb, k);
                pb = k;
                indexb = indexb + k;
            }
      
            // If the remaining string 
            // is of length less than k
            else if (lb - indexb < k && 
                     lb - indexb > 0) 
            {
                s = s + b.Substring(indexb, 
                               lb - indexb);
                pb = lb - indexb;
                indexb = lb;
            }
      
            // If the string has
            // been traversed
            else if (indexb >= lb)
                pb = 0;
            l = l - pa - pb;
        }
        Console.Write(s);
    }
      
    // Driver Code
    static void Main()
    {
        string a = "determination"
               b = "stance";
        int k = 3;
        solve(a, b, k);
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)

chevron_right



Output :

detstaermnceination


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : manishshaw1



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.