String from prefix and suffix of given two strings

20 May, 2021
Given two strings a and b, form a new string of length l, from these strings by combining the prefix of string a and suffix of string b.Examples :  
Input : string a = remuneration
        string b = acquiesce
        length of pre/suffix(l) = 5
Output :remuniesce

Input : adulation
        obstreperous
        6
Output :adulatperous
 
Approach : 1. Get first l letters from string a, and last l letters from string b. 2. Combine both results, and this will be resultant string. 
C++
Java
Python3
C#
PHP
Javascript
// CPP code to form new string from// pre/suffix of given strings.#include<bits/stdc++.h>using namespace std; // Returns a string which contains first l// characters of 'a' and last l characters of 'b'.string GetPrefixSuffix(string a, string b, int l){    // Getting prefix of first    // string of given length    string prefix = a.substr(0, l);         // length of string b    int lb = b.length();         // Calculating suffix of second string    string suffix = b.substr(lb - l);         // Concatenating both prefix and suffix    return (prefix + suffix);} // Driver codeint main(){    string a = "remuneration" ,           b = "acquiesce";    int l = 5;    cout << GetPrefixSuffix(a, b, l);    return 0;}
// Java Program to form new string// from pre/suffix of given stringsimport java.io.*; class GFG{    // Returns a string which contains first l    // characters of 'a' and last l characters of 'b'.    public static String prefixSuffix(String a,                                      String b,                                      int l)    {        // Calculating prefix of first        // string of given length        String prefix = a.substring(0, l);        int lb = b.length();         // Calculating suffix of second        // string of given length        String suffix = b.substring(lb - l);        return (prefix + suffix);    }         // Driver code    public static void main(String args[])                            throws IOException    {        String a = "remuneration" ,               b = "acquiesce";        int l = 5;        System.out.println(prefixSuffix(a, b, l));    }}
# Python code to form new from# pre/suffix of given strings. # Returns a string which contains first l# characters of 'a' and last l characters of 'b'.def GetPrefixSuffix(a, b, l):    # Getting prefix of first    # of given length    prefix = a[: l];         # length of string b    lb = len(b);         # Calculating suffix of second string    suffix = b[lb - l:];         # Concatenating both prefix and suffix    return (prefix + suffix);  # Driver codea = "remuneration";b = "acquiesce";l = 5;print(GetPrefixSuffix(a, b, l));  # This code contributed by Rajput-Ji
// C# Program to form new string// from pre/suffix of given strings.using System; class GFG{    // Returns a string which contains first l    // characters of 'a' and last l characters of 'b'.    public static String prefixSuffix(String a,                                      String b,                                      int l)    {        // Calculating prefix of first        // string of given length        String prefix = a.Substring(0, l);        int lb = b.Length;         // Calculating suffix of second        // string of given length        String suffix = b.Substring(lb - l);        return (prefix + suffix);    }         // Driver Code    public static void Main()    {        String a = "remuneration" ,               b = "acquiesce";        int l = 5;        Console.Write(prefixSuffix(a, b, l));    }} // This code is contributed by Nitin Mittal.
<?php// PHP code to form new string from// pre/suffix of given strings. // Returns a string which contains// first l characters of 'a' and// last l characters of 'b'.function GetPrefixSuffix($a, $b, $l){         // Getting prefix of first    // string of given length    $prefix = substr($a, 0, $l);         // length of string b    $lb = strlen($b);         // Calculating suffix of    // second string    $suffix = substr($b, $lb - $l);         // Concatenating both    // prefix and suffix    return ($prefix.$suffix);}     // Driver code    $a = "remuneration";    $b = "acquiesce";    $l = 5;    echo GetPrefixSuffix($a, $b, $l);     // This code is contributed by Sam007?>
<script> // JavaScript Program to form new string// from pre/suffix of given strings // Returns a string which contains first l// characters of 'a' and last l characters of 'b'.function prefixSuffix(a, b, l){    // Calculating prefix of first    // string of given length    var prefix = a.substring(0, l);    var lb = b.length;     // Calculating suffix of second    // string of given length    var suffix = b.substring(lb - l);    return (prefix + suffix);} // Driver code var a = "remuneration" ,        b = "acquiesce"; var l = 5; document.write(prefixSuffix(a, b, l));  // This code contributed by shikhasingrajput </script>
Output:  
remuniesce
 
String from prefix and suffix of given two strings | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersString from prefix and suffix of given two strings | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 1:49•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=6PMRRgQ5WWQ" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
nitin mittal
Sam007
Rajput-Ji
Akanksha_Rai
shikhasingrajput
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python program to check if a string is palindrome or not
Longest Palindromic Substring | Set 1
Length of the longest substring without repeating characters
Top 50 String Coding Problems for Interviews
Convert string to char array in C++
Check whether two strings are anagram of each other
Reverse words in a given string
What is Data Structure: Types, Classifications and Applications
Print all the duplicates in the input string
Reverse string in Python (6 different ways)